我正在开发一个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应用程序上遇到自定义错误问题.我创建了一个ErrorController并将以下代码添加到Global.asax以捕获未处理的异常,记录它们,然后将控制转移到ErrorController以显示自定义错误.此代码取自此处:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Response.Clear();
HttpException httpEx = ex as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpEx == null)
{
routeData.Values.Add("action", "Index");
}
else
{
switch (httpEx.GetHttpCode())
{
case 404:
routeData.Values.Add("action", "HttpError404");
break;
case 500:
routeData.Values.Add("action", "HttpError500");
break;
case 503:
routeData.Values.Add("action", "HttpError503");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
ExceptionLogger.LogException(ex); // <- This is working. Errors get logged
routeData.Values.Add("error", ex);
Server.ClearError();
IController controller = new ErrorController(); …Run Code Online (Sandbox Code Playgroud) 我有一个asp.net mvc应用程序,我正在尝试使用IISExpress进行自定义错误.
适用于Casini罚款:
<customErrors mode="On" defaultRedirect="/error">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)
当我之前将mvc站点部署到IIS(7.5)之后,我所要做的就是设置我的自定义错误:
<httpErrors errorMode="Detailed"/>
Run Code Online (Sandbox Code Playgroud)
我已经尝试在httpErrors部分中明确指定状态代码,但没有任何作用.这是一个例子:
<httpErrors errorMode="Detailed" defaultResponseMode="Redirect">
<clear/>
<error statusCode="404" path="/error/notfound"/>
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
谢谢Ben