如何正确处理ASP.NET MVC中的控制器抛出的异常?HandleError
属性似乎只处理MVC基础设施抛出的异常,而不是我自己的代码抛出的异常。
使用此web.config
<customErrors mode="On">
<error statusCode="401" redirect="/Errors/Http401" />
</customErrors>
使用以下代码
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
// Force a 401 exception for testing
throw new HttpException(401, "Unauthorized");
}
}
}
并没有得到我所希望的结果。相反,我得到了通用的ASP.NET错误页面,告诉我修改我的web.config以查看实际的错误信息。但是,如果我没有抛出异常,而是返回了一个无效的视图,则会得到/Shared/Views/Error.aspx页面:
return View("DoesNotExist");
在控制器中抛出异常,就像我上面所做的那样,似乎绕过了所有的HandleError
功能,那么创建错误页面的正确方法是什么,以及如何更好地使用MVC基础设施?
发布于 2009-05-01 19:51:06
多亏了kazimanzurrashaid,下面是我在Global.asax.cs中做的事情:
protected void Application_Error()
{
Exception unhandledException = Server.GetLastError();
HttpException httpException = unhandledException as HttpException;
if (httpException == null)
{
Exception innerException = unhandledException.InnerException;
httpException = innerException as HttpException;
}
if (httpException != null)
{
int httpCode = httpException.GetHttpCode();
switch (httpCode)
{
case (int) HttpStatusCode.Unauthorized:
Response.Redirect("/Http/Error401");
break;
}
}
}
我将能够根据我需要支持的任何额外的HTTP错误代码向HttpContoller中添加更多页面。
发布于 2009-05-01 17:32:15
Controller.OnException(ExceptionContext context)
。覆盖它。
protected override void OnException(ExceptionContext filterContext)
{
// Bail if we can't do anything; app will crash.
if (filterContext == null)
return;
// since we're handling this, log to elmah
var ex = filterContext.Exception ?? new Exception("No further information exists.");
LogException(ex);
filterContext.ExceptionHandled = true;
var data = new ErrorPresentation
{
ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
TheException = ex,
ShowMessage = !(filterContext.Exception == null),
ShowLink = false
};
filterContext.Result = View("ErrorPage", data);
}
发布于 2009-05-01 18:52:15
我认为您不能根据带有HandleError属性的HttpCode显示特定的ErrorPage,为此,我更喜欢使用HttpModule。假设我有一个文件夹"ErrorPages“,其中存在每个特定错误的不同页面,并且在web.config中指定了与常规web表单应用程序相同的映射。下面是用于显示错误页面的代码:
public class ErrorHandler : BaseHttpModule{
public override void OnError(HttpContextBase context)
{
Exception e = context.Server.GetLastError().GetBaseException();
HttpException httpException = e as HttpException;
int statusCode = (int) HttpStatusCode.InternalServerError;
// Skip Page Not Found and Service not unavailable from logging
if (httpException != null)
{
statusCode = httpException.GetHttpCode();
if ((statusCode != (int) HttpStatusCode.NotFound) && (statusCode != (int) HttpStatusCode.ServiceUnavailable))
{
Log.Exception(e);
}
}
string redirectUrl = null;
if (context.IsCustomErrorEnabled)
{
CustomErrorsSection section = IoC.Resolve<IConfigurationManager>().GetSection<CustomErrorsSection>("system.web/customErrors");
if (section != null)
{
redirectUrl = section.DefaultRedirect;
if (httpException != null)
{
if (section.Errors.Count > 0)
{
CustomError item = section.Errors[statusCode.ToString(Constants.CurrentCulture)];
if (item != null)
{
redirectUrl = item.Redirect;
}
}
}
}
}
context.Response.Clear();
context.Response.StatusCode = statusCode;
context.Response.TrySkipIisCustomErrors = true;
context.ClearError();
if (!string.IsNullOrEmpty(redirectUrl))
{
context.Server.Transfer(redirectUrl);
}
}
}
https://stackoverflow.com/questions/812235
复制相似问题