C# 需要帮助设置Swashback上的默认API路由(AzureFunctions)
So TL;DR.我一直在学习如何将ShwashBulk合并到Azure函数中,到目前为止它工作得很好,但我无法更改默认的路由前缀 我可以更改许多其他属性,如标题、服务器等,但如果有意义的话,我确实需要将默认api显示为just/path而不是/api/path 我将在下面发布我的代码,希望你们中的一些人能够帮助我 功能:C# 需要帮助设置Swashback上的默认API路由(AzureFunctions),c#,.net-core,swagger,azure-functions,swashbuckle,C#,.net Core,Swagger,Azure Functions,Swashbuckle,So TL;DR.我一直在学习如何将ShwashBulk合并到Azure函数中,到目前为止它工作得很好,但我无法更改默认的路由前缀 我可以更改许多其他属性,如标题、服务器等,但如果有意义的话,我确实需要将默认api显示为just/path而不是/api/path 我将在下面发布我的代码,希望你们中的一些人能够帮助我 功能: namespace SwashBuckleExample.Functions.Functions { public class GetClubByClubNameHt
namespace SwashBuckleExample.Functions.Functions
{
public class GetClubByClubNameHttp
{
private ISwashBuckleExampleProcess _process;
private ILogger<GetClubByClubNameHttp> _logger;
public GetClubByClubNameHttp(ISwashBuckleExampleProcess process, ILogger<GetClubByClubNameHttp> logger)
{
_process = process;
_logger = logger;
}
/// <summary>
/// Function that runs GetClubByNameProcess
/// </summary>
/// <param name="req">HttpRequest</param>
/// <returns>SwashBuckleExampleSuccessResponse Item</returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SwashBuckleExampleSuccessResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(SwashBuckleExampleBaseResponse))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(SwashBuckleExampleBaseResponse))]
[FunctionName("GetClubByClubNameHttp")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
[RequestBodyType(typeof(SwashBuckleExampleClubNameRequest), "Contains Club Name used for searching purposes")]HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function UpdateClientHttp");
//Read Query parameters
//Read RequestBody
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
//Decouple Requests
SwashBuckleExampleClubNameRequest request = JsonConvert.DeserializeObject<SwashBuckleExampleClubNameRequest>(requestBody);
//Make all pipeline Request
var responseObject = _process.GetClubByNameProcess(request);
return !responseObject.HasErrors ? (ObjectResult)new OkObjectResult(responseObject) : (ObjectResult)new BadRequestObjectResult(responseObject);
}
}
}
名称空间SwashBuckleExample.Functions.Functions
{
公共类GetClubByClubNameHttp
{
私有ISwashBuckleExampleProcess\u进程;
私人ILogger_记录器;
公共GetClubByClubNameHttp(ISwashBuckleExampleProcess进程,ILogger记录器)
{
_过程=过程;
_记录器=记录器;
}
///
///运行GetClubByNameProcess的函数
///
///HttpRequest
///SwashBuckleExampleSuccessResponse项目
[产品响应类型((int)HttpStatusCode.OK,Type=typeof(SwashbuckleeExampleSuccessResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError,Type=typeof(SwashBuckleExampleBaseResponse))]
[ProductsResponseType((int)HttpStatusCode.BadRequest,Type=typeof(SwashBuckleExampleBaseResponse))]
[FunctionName(“GetClubByClubNameHttp”)]
公共异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“post”,Route=null)]
[RequestBodyType(typeof(SwashbuckleeExampleClubNameRequest),“包含用于搜索目的的俱乐部名称”)]HttpRequest请求)
{
_logger.LogInformation(“C#HTTP触发器函数UpdateClientHttp”);
//读取查询参数
//读取请求体
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
//分离请求
SwashBuckleExampleClubNameRequest=JsonConvert.DeserializeObject(requestBody);
//发出所有管道请求
var responseObject=_process.GetClubByNameProcess(请求);
return!responseObject.HasErrors?(ObjectResult)new OkObjectResult(responseObject):(ObjectResult)new BadRequestObjectResult(responseObject);
}
}
}
虚张声势:
using System.Reflection;
using AzureFunctions.Extensions.Swashbuckle;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using SwashBuckleExample.Functions;
using SwashBuckleExample.Functions.SwaggerFunctions.SwaggerDocumentFilter;
[assembly: WebJobsStartup(typeof(SwashBuckleStartup))]
namespace SwashBuckleExample.Functions
{
internal class SwashBuckleStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
//Register the extension
builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), c =>
{
c.ConfigureSwaggerGen = (d => d.DocumentFilter<SwashBuckleExampleDocumentFilter>());
});
}
}
}
使用系统反射;
使用AzureFunctions.Extensions.Swashblock;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Hosting;
使用Microsoft.Extensions.DependencyInjection;
使用SwashBuckleExample.Functions;
使用SwashBuckleExample.Functions.SwaggerFunctions.SwaggerDocumentFilter;
[组装:WebJobsStartup(类型(SwashBuckleStartup))]
命名空间SwashBuckleExample.Functions
{
内部类SwashBuckleStartup:IWebJobsStartup
{
公共void配置(IWebJobsBuilder生成器)
{
//注册分机
builder.AddSwashBuckle(Assembly.getExecutionGassembly(),c=>
{
c、 ConfigureSwaggerGen=(d=>d.DocumentFilter());
});
}
}
}
文档过滤器:
namespace SwashBuckleExample.Functions.SwaggerFunctions.SwaggerDocumentFilter
{
public class SwashBuckleExampleDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Info.Title = "SwashBuckleExample";
swaggerDoc.Info.Version = "v1";
swaggerDoc.Servers = new List<OpenApiServer>() { new OpenApiServer() { Url = "LinkToServer" } };
}
}
}
namespace SwashBuckleExample.Functions.SwaggerFunctions.SwaggerDocumentFilter
{
公共类SwashBuckleExampleDocumentFilter:IDocumentFilter
{
公共作废应用(OpenApiDocument swaggerDoc、DocumentFilterContext上下文)
{
swaggerDoc.Info.Title=“SwashbuckleeExample”;
swaggerDoc.Info.Version=“v1”;
swaggerDoc.Servers=new List(){new OpenApiServer(){Url=“LinkToServer”};
}
}
}
谢谢你的帮助
附言:
我需要删除这个经常出现的参数,我不知道为什么。有什么办法吗?
1。如何更改Azure函数默认路由前缀。
如果我们想更改Azure函数的默认路由前缀,我们可以通过设置host.json中指定的routePrefix来实现它。有关更多详细信息,请参阅和。
比如说
在host.json
文件中添加以下设置
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix" : ""
}
},
....
}
2。为什么您的swagger文件有查询参数code
?
出现这种情况的原因是您使用代码HttpTrigger(AuthorizationLevel.function,“post”,Route=null)]
将Azure函数HTTP trigger auth级别设置为function或Admin。这样做之后,我们需要在调用函数时提供要包含在名为code
的查询字符串变量中的函数键或主机键。它的URL应该类似于https://
比如说
我的代码
[ProducesResponseType(typeof(TestModel[]), (int)HttpStatusCode.OK)]
[FunctionName("TestGets")]
public async Task<IActionResult> Gets([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "test")]
HttpRequest request)
{
return new OkObjectResult(new[] { new TestModel(), new TestModel() });
}
[ProducesResponseType(typeof(TestModel), (int)HttpStatusCode.Created)]
[FunctionName("TestAdd")]
public Task<IActionResult> Add([HttpTrigger(AuthorizationLevel.Function, "post", Route = "test")]
TestModel testModel)
{
return Task.FromResult<IActionResult>(new CreatedResult("", testModel));
}
如果没有AuthorizationLevel.Anonymous,是否无法删除参数?我们通常将我们的函数上传到FunctionApp,并通过Azure Api管理调用它,保留这些代码是一个很好的安全措施,我只是不想让它炫耀。
ProducesResponseType(typeof(TestModel), (int)HttpStatusCode.Created)]
[FunctionName("TestAdd")]
public Task<IActionResult> Add([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "test")]
TestModel testModel)
{
return Task.FromResult<IActionResult>(new CreatedResult("", testModel));
}