【发布时间】:2020-06-02 18:19:52
【问题描述】:
所以 TL;DR 我一直在学习如何将 shwashbuckle 合并到 Azure Functions 中,到目前为止它运行良好,但我无法更改我的默认路由前缀。
我能够更改许多其他属性,例如标题、服务器等,但我确实需要默认 api 以 /path 而不是 /api/path 的形式出现,如果这有意义的话。
我将在下面发布我的代码,希望你们中的一些人能够帮助我。
功能:
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);
}
}
}
SwashBuckleStartup:
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>());
});
}
}
}
文档过滤器:
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" } };
}
}
}
感谢您的帮助。
ps:
我需要删除这个总是出现但我不知道为什么的参数。有什么办法吗? Parameters
【问题讨论】:
标签: c# .net-core swagger azure-functions swashbuckle