gpt4 book ai didi

asp.net-core - 相当于ASP.NET Core的Response.TrySkipIisCustomErrors吗?

转载 作者:行者123 更新时间:2023-12-01 05:56:12 24 4
gpt4 key购买 nike

如何防止IIS使用IIS默认错误页面覆盖自定义错误页面?是否有等效于ASP.NET Core的Response.TrySkipIisCustomErrors?
在ASP Net MVC中,我使用下面的代码在没有自定义页面的情况下发送错误,但是在ASP Net Core中,它无法正常工作。

try
{
// some code
}
catch (Exception ex)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
mensagem = ex.Message;
}

最佳答案

您可以尝试编写一个exception处理中间件。这是一个blog post,我已将其用作引用。遵循以下原则

    public class ErrorHandlingMiddleware
{
private readonly RequestDelegate next;

public ErrorHandlingMiddleware(RequestDelegate next)
{
this.next = next;
}

public async Task Invoke(HttpContext context)
{
try
{
await next(context);
}
catch (Exception ex)
{
await CustomHandleExceptionAsync(context, ex);
}
}

private static Task CustomHandleExceptionAsync(HttpContext context, Exception exception)
{

if (exception is NotFoundException)
{

var customJson = JsonConvert.SerializeObject(new{ error =
exception.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
return context.Response.WriteAsync(customJson);
}
}

关于asp.net-core - 相当于ASP.NET Core的Response.TrySkipIisCustomErrors吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49269381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com