MVC6中的自定义选择性404页面 [英] Custom Selective 404 Page in MVC6
问题描述
我正在寻求帮助,我们可以在ASP.NET 5 MVC6中创建一个自定义的Error404页面.
I am looking for help we can have a custom Error404 page in ASP.NET 5 MVC6.
我最接近的是使用
app.UseStatusCodePagesWithReExecute("/Error/{0}");
在Startup.cs配置方法中.
in the Startup.cs Configure method.
通过调用"/Error/404"操作可以正常工作.
It works fine by calling the "/Error/404" action.
我正在寻找有关在缺少JPG/PNG/GIF请求的情况下如何发送不同的404响应的方法.
I am looking on ways on how I can send a different 404 response in the event it is a missing JPG/PNG/GIF request.
任何朝着正确方向提出的建议都会有很大帮助.
Any suggestion in the right direction would be of great help.
推荐答案
作为自定义中间件方法的替代方法,您可以使用MapWhen拆分管道并分别处理图像:
As an alternative to the custom middleware approach, you can use MapWhen to split the pipeline and handle images separately:
将以下内容添加到Startup.cs的Configure方法中(非图像中间件上方):
Add the following to the Configure method of Startup.cs (above your non-image middleware):
app.MapWhen(context => context.Request.Path.Value.EndsWith(".png"), appBuilder =>
{
appBuilder.UseStatusCodePagesWithReExecute("/image-error");
appBuilder.UseStaticFiles();
});
很明显,您可以更改谓词来满足您的需求.
Obviously, you can change the predicate to match your needs.
然后,您可以创建一个操作并查看该错误以处理该错误:
Then you can create an action and view to handle the error:
[Route("image-error")]
public IActionResult ImageError(int code)
{
return View();
}
您可以在我写的有关 查看全文