需要帮助我在SwashBuckle(AzureFunctions)上设置默认的API路由。

问题描述 投票:0回答:1

所以TL;DR我一直在学习如何将shwashbuckle纳入Azure Functions,到目前为止,它已经工作得很好,但我不能改变我的默认路由前缀。

我能够改变一些其他属性,如标题,服务器等,但我真的需要默认的api出来只是路径而不是apipath,如果这是有意义的。

我把我的代码贴在下面,希望你们中的一些人能够帮助我。

函数。

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>());
            });            
        }
    }
}

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" } };
        }
    }
}

谢谢你的帮助。

ps:

我需要删除这个总是显示的参数,我不知道为什么。有什么办法吗?参数

c# .net-core swagger azure-functions swashbuckle
1个回答
0
投票

1. 如何更改Azure函数默认路由前缀。

如果我们想改变Azure函数默认路由前缀,我们可以通过设置来实现。途径前缀 中指定的 host.json. 更多详情,请参考 此处此处.enter image description here

例如

在您的设置中添加以下设置 host.json 文件。

{
  "version": "2.0",
  "extensions": {

    "http": {
      "routePrefix" : ""
    }
  },
  ....
}

enter image description here

2. 为什么你的swagger文件中的查询参数是 code?

出现这种情况的原因是,你将Azure函数HTTP触发的auth级别设置为 功能管理员 带代码 HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]. 做完这些后,我们需要提供函数键或主机键,并将其包含在查询字符串变量中,该变量名为 code 当我们调用该函数时。它的URL应该是这样的https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>. 更多详情,请参考 文件

比如说

我的代码

[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));
        }

enter image description here

所以,如果你想删除这个参数,请更新代码为 [HttpTrigger(AuthorizationLevel.Anonymous,...)

例如我的代码

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));
        }

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.