ASP.NET MVC 3上的自定义错误页我正在开发一个MVC 3基础网站,我正在寻找一个处理错误的解决方案,并为每种错误呈现自定义视图。因此,假设我有一个“Error”Controller,其中他的主要操作是“Index”(泛型错误页),并且对于用户可能出现的错误,这个控制器还会有几个操作,比如“Handle 500”或“HandleActionNotFound”。因此,网站上可能发生的每一个错误都可能由这个“Error”Controller(例如:“Controller”或“Action”未找到、500、404、dbException等)处理。我使用站点地图文件来定义网站路径(而不是路由)。这个问题已经回答了,这是对Gweebz的答复。我的最后一个应用程序错误方法如下: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, we will display static HTML error page
Log.Logger.Fatal("failed to display error page, fallback to HTML error: ", e);
Response.TransmitFile("~/error.html");
}}}
3 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
ErrorsController
public class ErrorsController : Controller{ public ActionResult General(Exception exception) { return Content("General failure", "text/plain"); } public ActionResult Http404() { return Content("Not found", "text/plain"); } public ActionResult Http403() { return Content("Forbidden", "text/plain"); }}
Application_Error
Global.asax
protected void Application_Error(){ var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc);}
慕田峪9158850
TA贡献1794条经验 获得超7个赞
<system.webServer> <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File"> <remove statusCode="502" subStatusCode="-1" /> <remove statusCode="501" subStatusCode="-1" /> <remove statusCode="412" subStatusCode="-1" /> <remove statusCode="406" subStatusCode="-1" /> <remove statusCode="405" subStatusCode="-1" /> <remove statusCode="404" subStatusCode="-1" /> <remove statusCode="403" subStatusCode="-1" /> <remove statusCode="401" subStatusCode="-1" /> <remove statusCode="500" subStatusCode="-1" /> <error statusCode="500" path="/notfound.html" responseMode="ExecuteURL" /> <error statusCode="401" prefixLanguageFilePath="" path="/500.html" responseMode="ExecuteURL" /> <error statusCode="403" prefixLanguageFilePath="" path="/403.html" responseMode="ExecuteURL" /> <error statusCode="404" prefixLanguageFilePath="" path="/404.html" responseMode="ExecuteURL" /> <error statusCode="405" prefixLanguageFilePath="" path="/405.html" responseMode="ExecuteURL" /> <error statusCode="406" prefixLanguageFilePath="" path="/406.html" responseMode="ExecuteURL" /> <error statusCode="412" prefixLanguageFilePath="" path="/412.html" responseMode="ExecuteURL" /> <error statusCode="501" prefixLanguageFilePath="" path="/501.html" responseMode="ExecuteURL" /> <error statusCode="502" prefixLanguageFilePath="" path="/genericerror.html" responseMode="ExecuteURL" /> </httpErrors></system.webServer>
- 3 回答
- 0 关注
- 616 浏览
添加回答
举报
0/150
提交
取消