Kin*_*nus 5 c# asp.net asp.net-mvc asp.net-mvc-2
我有一个MVC 2 Web应用程序,即将发布.到目前为止,我已关闭自定义错误,但我希望它们在我准备好生产时正常工作.
我已经使用以下内容设置了我的web.config:
<customErrors mode="On" defaultRedirect="/Error/">
<error statusCode="404" redirect="/Error/NotFound "/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)
404完美地工作,NotFound是一个直接映射到View的动作,它只使用我自己的Site.Master文件显示一个非常标准的404页面.
对于除404之外的任何内容,我希望用户能够查看异常详细信息.(这是一个内部应用程序,这样做没有安全风险).
该Error默认操作Index设置为返回视图(),我已经定义.我无法弄清楚如何将异常信息传递给View?
看起来很有希望:
http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/
但是当我使用View时:
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/Bootstrap.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
Run Code Online (Sandbox Code Playgroud)
由于HandleErrorInfo为null,错误页面本身会抛出错误.显然,自定义错误中的错误会导致MVC2出现大量问题,结果是黄色的死亡屏幕.
我想我要么错过了博客中的关键内容,要么就没有解释如何让HandleErrorInfo成为除null以外的任何东西,而不为我的每一个控制器/动作设置Error属性.
另外,我知道MVC3可以更好地处理这个问题,我知道Razor非常好.它尚未用于此项目,也不会更改此项目以使用它.所以请不要"使用MVC3"答案.
VJA*_*JAI 12
HandleErrorInfo由于您正在执行重定向,因此为null customErrors.
这是我在我的最新项目中尝试的想法,我更新了MVC 2.我没有使用,customErrors因为我无法在不执行重定向的情况下调用控制器操作(我猜).
应用程序错误
protected void Application_Error(Object sender, EventArgs e)
{
GlobalErrorHandler.HandleError(((HttpApplication)sender).Context, Server.GetLastError(), new ErrorController());
}
Run Code Online (Sandbox Code Playgroud)
全局错误处理程序
public class GlobalErrorHandler
{
public static void HandleError(HttpContext context, Exception ex, Controller controller)
{
LogException(ex);
context.Response.StatusCode = GetStatusCode(ex);
context.ClearError();
context.Response.Clear();
context.Response.TrySkipIisCustomErrors = true;
if (IsAjaxRequest(context.Request))
{
ReturnErrorJson(context, ex);
return;
}
ReturnErrorView(context, ex, controller);
}
public static void LogException(Exception ex)
{
// log the exception
}
private static void ReturnErrorView(HttpContext context, Exception ex, Controller controller)
{
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = GetActionName(GetStatusCode(ex));
controller.ViewData.Model = new HandleErrorInfo(ex, " ", " ");
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(context), routeData));
}
private static void ReturnErrorJson(HttpContext context, Exception ex)
{
var json = string.Format(@"success: false, error:""{0}""", ex.Message);
context.Response.ContentType = "application/json";
context.Response.Write("{" + json + "}");
}
private static int GetStatusCode(Exception ex)
{
return ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
}
private static bool IsAjaxRequest(HttpRequest request)
{
return request.Headers["X-Requested-With"] != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
}
private static string GetActionName(int statusCode)
{
string actionName;
switch (statusCode)
{
case 404:
actionName = "NotFound";
break;
case 400:
actionName = "InvalidRequest";
break;
case 401:
actionName = "AccessDenied";
break;
default:
actionName = "ServerError";
break;
}
return actionName;
}
public static bool IsDebug
{
get
{
bool debug = false;
#if DEBUG
debug = true;
#endif
return debug;
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误控制器
public class ErrorController : Controller
{
public ActionResult AccessDenied()
{
return View("AccessDenied");
}
public ActionResult InvalidRequest()
{
return View("InvalidRequest");
}
public ActionResult NotFound()
{
return View("NotFound");
}
public ActionResult ServerError()
{
return View("ServerError");
}
}
Run Code Online (Sandbox Code Playgroud)
ServerError视图
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ServerError
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ServerError</h2>
<% if (Model.Exception != null ) { %>
<p>
Controller: <%= Model.ControllerName %>
</p>
<p>
Action: <%= Model.ActionName %>
</p>
<p>
Message: <%= Model.Exception.Message%>
</p>
<p>
Stack Trace: <%= Model.Exception.StackTrace%>
</p>
<% } %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)