Azure函数没有';t返回405响应

Azure函数没有';t返回405响应,azure,azure-functions,http-status-code-405,Azure,Azure Functions,Http Status Code 405,当仅为POST定义HTTP触发器,但用户尝试对同一URL执行GET时,Azure函数将返回404而不是预期的405 在这种情况下,返回HTTP状态代码405的最佳方式是什么?(连同所需的“允许”标题) 有趣的是,APIM目前还返回404而不是405个响应()。我查看了很多文档和博客,但没有找到完美的解决方案。我有一个解决方案,它看起来有点愚蠢,但它是有效的 解决方案 您可以允许使用get请求,然后在函数中对其进行判断。如果是get请求,请手动指定405状态代码 完整的示例代码如下所示: usi

当仅为POST定义HTTP触发器,但用户尝试对同一URL执行GET时,Azure函数将返回404而不是预期的405

在这种情况下,返回HTTP状态代码405的最佳方式是什么?(连同所需的“允许”标题)


有趣的是,APIM目前还返回404而不是405个响应()。

我查看了很多文档和博客,但没有找到完美的解决方案。我有一个解决方案,它看起来有点愚蠢,但它是有效的

解决方案

您可以允许使用get请求,然后在
函数中对其进行判断。如果是
get
请求,请手动指定
405
状态代码

完整的示例代码如下所示:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace RespCodeFunc
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            if (req.Method == HttpMethods.Get) {
                var result = new ObjectResult("your message");
                result.StatusCode = StatusCodes.Status405MethodNotAllowed;
                return result;
            }
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
名称空间RespCodeFunc
{
公共静态类函数1
{
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
if(req.Method==HttpMethods.Get){
var result=新的ObjectResult(“您的消息”);
result.StatusCode=不允许的StatusCodes.Status405Method;
返回结果;
}
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串名称=请求查询[“名称”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
string responseMessage=string.IsNullOrEmpty(名称)
?“此HTTP触发的函数已成功执行。请在查询字符串或请求正文中为个性化响应传递名称。”
:$“您好,{name}。此HTTP触发的函数已成功执行。”;
返回新的OkObjectResult(responseMessage);
}
}
}
返回的状态代码应为:


从历史上看,这似乎是框架的一种行为,实际上是通过设计实现的。