gpt4 book ai didi

asp.net-core - MVC6 中的自定义选择性 404 页面

转载 作者:行者123 更新时间:2023-12-02 04:02:54 26 4
gpt4 key购买 nike

我正在寻求帮助,我们可以在 ASP.NET 5 MVC6 中创建自定义 Error404 页面。

我最接近的是使用

app.UseStatusCodePagesWithReExecute("/Error/{0}");

在 Startup.cs 配置方法中。

通过调用“/Error/404”操作可以正常工作。

我正在研究如何在缺少 JPG/PNG/GIF 请求的情况下发送不同的 404 响应。

任何正确方向的建议都会有很大帮助。

最佳答案

如果您使用当前的 UseStatusCodePages 中间件,它将影响每个错误。为了实现您正在寻找的目标,您需要创建自己的中间件,需要将其放置在默认错误处理中间件之后:

//Startup.cs
public void Configure(IApplicationBuilder app)
{
app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
app.UseImageNotFoundMiddlewareWithRedirect("/Home/ImageError");
}

这应该可以帮助您开始:Github - StatusCodePage

这是一个示例中间件,可以模拟您可能正在寻找的内容:

public class ImageNotFoundMiddleware
{
private readonly RequestDelegate _next;
private readonly StatusCodePagesOptions _options;

public ImageNotFoundMiddleware(RequestDelegate next, IOptions<StatusCodePagesOptions> options)
{
_next = next;
_options = options.Value;
if (_options.HandleAsync == null)
{
throw new ArgumentException("Missing options.HandleAsync implementation.");
}
}

public async Task Invoke(HttpContext context)
{
var statusCodeFeature = new StatusCodePagesFeature();
context.Features.Set<IStatusCodePagesFeature>(statusCodeFeature);

await _next(context);

if (!statusCodeFeature.Enabled)
{
// Check if the feature is still available because other middleware (such as a web API written in MVC) could
// have disabled the feature to prevent HTML status code responses from showing up to an API client.
return;
}

// Do nothing if a response body has already been provided or not 404 response
if (context.Response.HasStarted
|| context.Response.StatusCode != 404
|| context.Response.ContentLength.HasValue
|| !string.IsNullOrEmpty(context.Response.ContentType))
{
return;
}

// todo => Here is where you'd also add your logic to check for the image 404...
if (context.Request.Path.Value.EndsWith(".JPG", StringComparison.OrdinalIgnoreCase)
|| context.Request.Path.Value.EndsWith(".PNG", StringComparison.OrdinalIgnoreCase)
|| context.Request.Path.Value.EndsWith(".GIF", StringComparison.OrdinalIgnoreCase)
)
{
var statusCodeContext = new StatusCodeContext(context, _options, _next);
await _options.HandleAsync(statusCodeContext);
}
}
}

// Extension method used to add the middleware to the HTTP request pipeline.
public static class ImageNotFoundMiddlewareExtensions
{
public static IApplicationBuilder UseImageNotFoundMiddlewareWithRedirect(this IApplicationBuilder app,
string locationFormat)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}

return app.UseMiddleware<ImageNotFoundMiddleware>(
Options.Create(
new StatusCodePagesOptions
{
HandleAsync = context =>
{
var location = string.Format(
CultureInfo.InvariantCulture,
locationFormat.StartsWith("~") ? locationFormat.Substring(1) : locationFormat,
context.HttpContext.Response.StatusCode);
context.HttpContext.Response.Redirect(
locationFormat.StartsWith("~")
? context.HttpContext.Request.PathBase + location
: location);
return Task.FromResult(0);
}
}
)
);
}
}

关于asp.net-core - MVC6 中的自定义选择性 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34855688/

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