相当于 .NET 7 隔离工作线程模型 Azure Functions 中的 NotFoundObjectResult

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

我在 Azure Function 中有这段代码,我使用过 .NET 6.0:

catch (Exception ex)
{
    log.LogError($"Data Access Failed: {ex.Message}");

    return new NotFoundObjectResult(ex.Message);
}

我将Azure Function升级到.NET 7.0独立工作线程模型.NET 7.0 隔离工作模型中返回未找到数据的HTTP 状态代码的等效项是什么。

azure-functions http-status-code-400 asp.net-core-7.0
1个回答
0
投票

您可以使用类似下面的代码(

HttpStatusCode.NotFound
),它会给出 404 状态代码:

Code:

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp77
{
    public class Function1
    {
        private readonly ILogger _logger;
        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("Hello Rithwik your function is being processed");

            var response = req.CreateResponse(HttpStatusCode.NotFound);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
            response.WriteString("No data found Rithwik!");
            return response;
        }
    }
}

Output:

enter image description here

enter image description here

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