我想将自定义错误页面添加到我的项目中。
我发现这篇关于我的问题的文章 http://benfoster.io/blog/aspnet-mvc-custom-error-pages我尝试去实现它。
So :
- i add
404.cshtml
, 404.html
, 500.cshtml
and 500.html
pages
- 在添加的cshtml文件中设置响应状态代码
- 评论添加
HandleErrorAttribute
到全局过滤器
- 更新我的 web.config 文件
但现在当我尝试走小路时http://localhost:120/foo/bar
我的应用程序所在的位置http://localhost:120
我得到下一页:
“/”应用程序中的服务器错误。
运行时错误
描述:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页面时发生了另一个异常。请求已终止。
I set <customErrors mode="Off"
看看是什么问题。它是 -The resource cannot be found.
这是合乎逻辑的。但是当我设置<customErrors mode="On"
- 我再次收到运行时错误。
是什么原因造成的以及如何解决?
我的配置文件:
<system.web>
<customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/500.cshtml">
<error statusCode="404" redirect="~/404.cshtml"/>
<error statusCode="500" redirect="~/500.cshtml"/>
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="404.html" responseMode="File"/>
<remove statusCode="500"/>
<error statusCode="500" path="500.html" responseMode="File"/>
</httpErrors>
</system.webServer>
IIS 版本 8.5
您使用什么版本的 IIS?如果 7+ 则忽略自定义错误<customErrors mode="Off">
并使用<httpErrors>
。使用后者下面的方法将显示错误页面而不更改 URL,IMO 是处理这些事情的首选方法。
设置一个错误控制器并将 404 和 500 放入其中,如下所示:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
<error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
</httpErrors>
在控制器中:
public class ErrorController : Controller
{
public ActionResult servererror()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View();
}
public ActionResult notfound()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
}
然后显然为所覆盖的每个错误设置相应的视图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)