I had this code in Azure Function where I have used .NET 6.0:
catch (Exception ex)
{
log.LogError($"Data Access Failed: {ex.Message}");
return new NotFoundObjectResult(ex.Message);
}
I upgraded the Azure Function to .NET 7.0 Isolated Worker Model. What is the equivalent in .NET 7.0 isolated worker model to return the HTTP status code of no data found.
You can use something like below code(HttpStatusCode.NotFound
) which gives 404 status code:
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:
In response.WriteString
written my own message, there you can send exception message