我想为500、404和403显示一个自定义错误页面。以下是我所做的:
Enabled custom errors in the web.config as follows:
<customErrors mode="On"
defaultRedirect="~/Views/Shared/Error.cshtml">
<error statusCode="403"
redirect="~/Views/Shared/UnauthorizedAccess.cshtml" />
<error statusCode="404"
redirect="~/Views/Shared/FileNotFound.cshtml" />
</customErrors>
Registered HandleErrorAttribute as a global action filter in the FilterConfig class as follows:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new CustomHandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
Created a custom error page for each of the above messages. The default one for 500 was already available out of the box.
Declared in each custom error page view that the model for the page is System.Web.Mvc.HandleErrorInfo
对于500,它显示自定义错误页面。对其他人来说,则不然。
我是不是遗漏了什么?
当我阅读HandleErrorAttribute类的OnException方法中的代码时,它看起来并不是显示自定义错误的全部,而且它只处理500个。
我必须做什么来处理其他错误?
在网络上。在系统下添加此配置。Webserver标签如下所示,
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<remove statusCode="500"/>
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound"/>
<error statusCode="500" responseMode="ExecuteURL"path="/Error/ErrorPage"/>
</httpErrors>
并添加控制器为,
public class ErrorController : Controller
{
//
// GET: /Error/
[GET("/Error/NotFound")]
public ActionResult NotFound()
{
Response.StatusCode = 404;
return View();
}
[GET("/Error/ErrorPage")]
public ActionResult ErrorPage()
{
Response.StatusCode = 500;
return View();
}
}
加上他们受人尊敬的观点,我想这对所有人都肯定有效。
这个解决方案是我从《Neptune Century》中找到的
你可以得到正确工作的错误,而不入侵global.cs,扰乱HandleErrorAttribute,做响应。TrySkipIisCustomErrors,连接Application_Error,或者别的什么:
在系统。Web(只是平常的,开/关)
<customErrors mode="On">
<error redirect="/error/401" statusCode="401" />
<error redirect="/error/500" statusCode="500" />
</customErrors>
在system.webServer中
<httpErrors existingResponse="PassThrough" />
现在事情的表现应该和预期的一样了,您可以使用ErrorController来显示您需要的任何东西。