我正在开发一个MVC3基础网站,我正在寻找一个处理错误的解决方案,并为每种错误渲染自定义视图.因此,假设我有一个"错误"控制器,其主要操作是"索引"(通用错误页面),并且此控制器将针对用户可能出现的错误(如"Handle500"或"HandleActionNotFound")执行更多操作.
因此,网站上可能发生的每个错误都可能由此"错误"控制器处理(例如:"Controller"或"Action"未找到,500,404,dbException等).
我使用Sitemap文件来定义网站路径(而不是路由).
这个问题已经回答了,这是对Gweebz的回复
我的最终applicaiton_error方法如下:
protected void Application_Error() {
//while my project is running in debug mode
if (HttpContext.Current.IsDebuggingEnabled && WebConfigurationManager.AppSettings["EnableCustomErrorPage"].Equals("false"))
{
Log.Logger.Error("unhandled exception: ", Server.GetLastError());
}
else
{
try
{
var exception = Server.GetLastError();
Log.Logger.Error("unhandled exception: ", exception);
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
catch (Exception e)
{
//if Error controller failed for same reason, …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在ASP.NET MVC中正确处理404?
我已经在Asp.Net MVC(RC 5)中的404 Http错误处理程序中进行了更改,我仍然得到标准的404错误页面.我需要在IIS中更改某些内容吗?
我希望将所有401错误重定向到自定义错误页面.我最初在web.config中设置了以下条目.
<customErrors defaultRedirect="ErrorPage.aspx" mode="On">
<error statusCode="401" redirect="~/Views/Shared/AccessDenied.aspx" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
使用IIS Express时,我收到了IIS Express 401错误页面.
如果我不使用IIS Express,则返回空白页面.使用Google Chrome的"网络"标签检查响应,我看到当页面为空白时,标题中会返回401状态
到目前为止我尝试使用的是这个SO答案中的建议,因为我使用的是IIS Express,但无济于事.我尝试过使用组合<custom errors>而<httpErrors>没有运气 - 标准错误或空白页仍然显示.
该httpErrors部分看起来像这样的基础上,瞬间链接从上面的SO问题(我还发现了另一种非常有前途的答案却没有运气-空白响应)
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >
<remove statusCode="401" />
<error statusCode="401" path="/Views/Shared/AccessDenied.htm" />
</httpErrors>
<!--
<httpErrors errorMode="Custom"
existingResponse="PassThrough"
defaultResponseMode="ExecuteURL">
<remove statusCode="401" />
<error statusCode="401" path="~/Views/Shared/AccessDenied.htm"
responseMode="File" />
</httpErrors>
-->
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
我甚至已经修改了applicationhost.config文件,并修改<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">,以<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated">基于从信息iis.net.在我努力的过程中,我也设法偶然发现了另一个SO问题中描述的这个错误.
如何在Asp.Net Mvc …
error-handling http-error custom-error-pages iis-express asp.net-mvc-3