Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MVC错误处理IIS Express和IIS 7.5_C#_Asp.net Mvc 4_Iis_Error Handling - Fatal编程技术网

C#MVC错误处理IIS Express和IIS 7.5

C#MVC错误处理IIS Express和IIS 7.5,c#,asp.net-mvc-4,iis,error-handling,C#,Asp.net Mvc 4,Iis,Error Handling,是否存在一个完整的解决方案来真正处理404、500等错误。。。在IIS Express和IIS 7.5上 我已经记不清我读过多少篇文章,关于Web.config,打开或关闭customErrors等等。。。或注释/取消注释filters.Add(新HandleErrorAttribute())来自FilterConfig.cs 有人能回顾一下我到目前为止所做的工作,并告诉我正确的配置是什么,使我能够完全捕获IIS Express和IIS 7.5上的服务器错误,显示自定义错误页面,而不是无论发生什

是否存在一个完整的解决方案来真正处理404、500等错误。。。在IIS Express和IIS 7.5上

我已经记不清我读过多少篇文章,关于Web.config,打开或关闭customErrors等等。。。或注释/取消注释
filters.Add(新HandleErrorAttribute())来自FilterConfig.cs

有人能回顾一下我到目前为止所做的工作,并告诉我正确的配置是什么,使我能够完全捕获IIS Express和IIS 7.5上的服务器错误,显示自定义错误页面,而不是无论发生什么情况都会调用的Shared/error.cshtml,并且忽略应用程序错误

Global.asax.cs

protected void Application_Error(object sender, EventArgs e)
{
    var lastError = Server.GetLastError();

    Server.ClearError();

    var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;

    var httpContextWrapper = new HttpContextWrapper(Context);

    var routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Index");
    routeData.Values.Add("statusCode", statusCode);
    routeData.Values.Add("exception", lastError);

    IController errorController = new ErrorController();

    var requestContext = new RequestContext(httpContextWrapper, routeData);

    errorController.Execute(requestContext);

    Response.End();
}
public class ErrorController : Controller
{
    private readonly Common _cf = new Common();
    private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
    private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

    public ActionResult Index(int? statusCode, Exception exception)
    {
        Response.TrySkipIisCustomErrors = true;

        try
        {
            Response.StatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception tryException)
        {
            SendEmail(tryException, statusCode.ToString());

            return Redirect("/");
        }

        SendEmail(exception, Convert.ToString(Response.StatusCode));

        ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";

        ViewBag.ExceptionMessage = Convert.ToString(exception.Message);

        return View("Index");
    }

    private void SendEmail(Exception exception, string errorType)
    {
        const string to = "to@domain.com";
        const string @from = "from@domain.com";

        var subject = "SendEmailError (" + errorType + ")";

        var stringBuilder = new StringBuilder();            
        stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
        stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
        stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
        stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
        stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
        stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
        stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
        stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");

        var body = stringBuilder.ToString();

        _cf.SendEmail(subject, to, from, body, null, true, null, null);
    }
}
Exception ex = Server.GetLastError();
public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea
<customErrors mode="On" defaultRedirect="~/ErrorPage/DisplayError/500">
  <error redirect="~/ErrorPage/DisplayError/403" statusCode="403" />
  <error redirect="~/ErrorPage/DisplayError/404" statusCode="404" />
  <error redirect="~/ErrorPage/DisplayError/500" statusCode="500" />
</customErrors>
public class BaseController : Controller
{
    protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
    {
        return new HttpUnauthorizedResult(statusDescription);
    }

    protected class HttpNotFoundResult : HttpStatusCodeResult
    {
        public HttpNotFoundResult() : this(null) { }

        public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
    }

    protected class HttpUnauthorizedResult : HttpStatusCodeResult
    {
        public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
    }

    protected class HttpStatusCodeResult : ViewResult
    {
        public int StatusCode { get; private set; }
        public string StatusDescription { get; private set; }

        public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }

        public HttpStatusCodeResult(int statusCode, string statusDescription)
        {
            StatusCode = statusCode;
            StatusDescription = statusDescription;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.HttpContext.Response.StatusCode = StatusCode;

            if (StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = StatusDescription;
            }

            ViewName = "Error";

            ViewBag.Title = context.HttpContext.Response.StatusDescription;

            base.ExecuteResult(context);
        }
    }
}
ErrorController.cs

protected void Application_Error(object sender, EventArgs e)
{
    var lastError = Server.GetLastError();

    Server.ClearError();

    var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;

    var httpContextWrapper = new HttpContextWrapper(Context);

    var routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Index");
    routeData.Values.Add("statusCode", statusCode);
    routeData.Values.Add("exception", lastError);

    IController errorController = new ErrorController();

    var requestContext = new RequestContext(httpContextWrapper, routeData);

    errorController.Execute(requestContext);

    Response.End();
}
public class ErrorController : Controller
{
    private readonly Common _cf = new Common();
    private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
    private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

    public ActionResult Index(int? statusCode, Exception exception)
    {
        Response.TrySkipIisCustomErrors = true;

        try
        {
            Response.StatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception tryException)
        {
            SendEmail(tryException, statusCode.ToString());

            return Redirect("/");
        }

        SendEmail(exception, Convert.ToString(Response.StatusCode));

        ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";

        ViewBag.ExceptionMessage = Convert.ToString(exception.Message);

        return View("Index");
    }

    private void SendEmail(Exception exception, string errorType)
    {
        const string to = "to@domain.com";
        const string @from = "from@domain.com";

        var subject = "SendEmailError (" + errorType + ")";

        var stringBuilder = new StringBuilder();            
        stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
        stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
        stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
        stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
        stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
        stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
        stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
        stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");

        var body = stringBuilder.ToString();

        _cf.SendEmail(subject, to, from, body, null, true, null, null);
    }
}
Exception ex = Server.GetLastError();
public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea
<customErrors mode="On" defaultRedirect="~/ErrorPage/DisplayError/500">
  <error redirect="~/ErrorPage/DisplayError/403" statusCode="403" />
  <error redirect="~/ErrorPage/DisplayError/404" statusCode="404" />
  <error redirect="~/ErrorPage/DisplayError/500" statusCode="500" />
</customErrors>
public class BaseController : Controller
{
    protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
    {
        return new HttpUnauthorizedResult(statusDescription);
    }

    protected class HttpNotFoundResult : HttpStatusCodeResult
    {
        public HttpNotFoundResult() : this(null) { }

        public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
    }

    protected class HttpUnauthorizedResult : HttpStatusCodeResult
    {
        public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
    }

    protected class HttpStatusCodeResult : ViewResult
    {
        public int StatusCode { get; private set; }
        public string StatusDescription { get; private set; }

        public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }

        public HttpStatusCodeResult(int statusCode, string statusDescription)
        {
            StatusCode = statusCode;
            StatusDescription = statusDescription;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.HttpContext.Response.StatusCode = StatusCode;

            if (StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = StatusDescription;
            }

            ViewName = "Error";

            ViewBag.Title = context.HttpContext.Response.StatusDescription;

            base.ExecuteResult(context);
        }
    }
}

任何帮助都将不胜感激:-)

刚刚浏览了我最近做的一个项目。 我在Application_Error()中只有一行:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //filters.Add(new HandleErrorAttribute()); // Removed because ELMAH reports "cannot find Shared/Error" view instead of the exception.
    }
}
过滤器配置.cs

protected void Application_Error(object sender, EventArgs e)
{
    var lastError = Server.GetLastError();

    Server.ClearError();

    var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;

    var httpContextWrapper = new HttpContextWrapper(Context);

    var routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Index");
    routeData.Values.Add("statusCode", statusCode);
    routeData.Values.Add("exception", lastError);

    IController errorController = new ErrorController();

    var requestContext = new RequestContext(httpContextWrapper, routeData);

    errorController.Execute(requestContext);

    Response.End();
}
public class ErrorController : Controller
{
    private readonly Common _cf = new Common();
    private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
    private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

    public ActionResult Index(int? statusCode, Exception exception)
    {
        Response.TrySkipIisCustomErrors = true;

        try
        {
            Response.StatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception tryException)
        {
            SendEmail(tryException, statusCode.ToString());

            return Redirect("/");
        }

        SendEmail(exception, Convert.ToString(Response.StatusCode));

        ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";

        ViewBag.ExceptionMessage = Convert.ToString(exception.Message);

        return View("Index");
    }

    private void SendEmail(Exception exception, string errorType)
    {
        const string to = "to@domain.com";
        const string @from = "from@domain.com";

        var subject = "SendEmailError (" + errorType + ")";

        var stringBuilder = new StringBuilder();            
        stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
        stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
        stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
        stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
        stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
        stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
        stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
        stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");

        var body = stringBuilder.ToString();

        _cf.SendEmail(subject, to, from, body, null, true, null, null);
    }
}
Exception ex = Server.GetLastError();
public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea
<customErrors mode="On" defaultRedirect="~/ErrorPage/DisplayError/500">
  <error redirect="~/ErrorPage/DisplayError/403" statusCode="403" />
  <error redirect="~/ErrorPage/DisplayError/404" statusCode="404" />
  <error redirect="~/ErrorPage/DisplayError/500" statusCode="500" />
</customErrors>
public class BaseController : Controller
{
    protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
    {
        return new HttpUnauthorizedResult(statusDescription);
    }

    protected class HttpNotFoundResult : HttpStatusCodeResult
    {
        public HttpNotFoundResult() : this(null) { }

        public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
    }

    protected class HttpUnauthorizedResult : HttpStatusCodeResult
    {
        public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
    }

    protected class HttpStatusCodeResult : ViewResult
    {
        public int StatusCode { get; private set; }
        public string StatusDescription { get; private set; }

        public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }

        public HttpStatusCodeResult(int statusCode, string statusDescription)
        {
            StatusCode = statusCode;
            StatusDescription = statusDescription;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.HttpContext.Response.StatusCode = StatusCode;

            if (StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = StatusDescription;
            }

            ViewName = "Error";

            ViewBag.Title = context.HttpContext.Response.StatusDescription;

            base.ExecuteResult(context);
        }
    }
}
ErrorPageController.cs

protected void Application_Error(object sender, EventArgs e)
{
    var lastError = Server.GetLastError();

    Server.ClearError();

    var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;

    var httpContextWrapper = new HttpContextWrapper(Context);

    var routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Index");
    routeData.Values.Add("statusCode", statusCode);
    routeData.Values.Add("exception", lastError);

    IController errorController = new ErrorController();

    var requestContext = new RequestContext(httpContextWrapper, routeData);

    errorController.Execute(requestContext);

    Response.End();
}
public class ErrorController : Controller
{
    private readonly Common _cf = new Common();
    private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
    private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

    public ActionResult Index(int? statusCode, Exception exception)
    {
        Response.TrySkipIisCustomErrors = true;

        try
        {
            Response.StatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception tryException)
        {
            SendEmail(tryException, statusCode.ToString());

            return Redirect("/");
        }

        SendEmail(exception, Convert.ToString(Response.StatusCode));

        ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";

        ViewBag.ExceptionMessage = Convert.ToString(exception.Message);

        return View("Index");
    }

    private void SendEmail(Exception exception, string errorType)
    {
        const string to = "to@domain.com";
        const string @from = "from@domain.com";

        var subject = "SendEmailError (" + errorType + ")";

        var stringBuilder = new StringBuilder();            
        stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
        stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
        stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
        stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
        stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
        stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
        stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
        stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");

        var body = stringBuilder.ToString();

        _cf.SendEmail(subject, to, from, body, null, true, null, null);
    }
}
Exception ex = Server.GetLastError();
public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea
<customErrors mode="On" defaultRedirect="~/ErrorPage/DisplayError/500">
  <error redirect="~/ErrorPage/DisplayError/403" statusCode="403" />
  <error redirect="~/ErrorPage/DisplayError/404" statusCode="404" />
  <error redirect="~/ErrorPage/DisplayError/500" statusCode="500" />
</customErrors>
public class BaseController : Controller
{
    protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
    {
        return new HttpUnauthorizedResult(statusDescription);
    }

    protected class HttpNotFoundResult : HttpStatusCodeResult
    {
        public HttpNotFoundResult() : this(null) { }

        public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
    }

    protected class HttpUnauthorizedResult : HttpStatusCodeResult
    {
        public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
    }

    protected class HttpStatusCodeResult : ViewResult
    {
        public int StatusCode { get; private set; }
        public string StatusDescription { get; private set; }

        public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }

        public HttpStatusCodeResult(int statusCode, string statusDescription)
        {
            StatusCode = statusCode;
            StatusDescription = statusDescription;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.HttpContext.Response.StatusCode = StatusCode;

            if (StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = StatusDescription;
            }

            ViewName = "Error";

            ViewBag.Title = context.HttpContext.Response.StatusDescription;

            base.ExecuteResult(context);
        }
    }
}
Web.config

<system.webServer>
  <handlers>
    <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
  <httpErrors existingResponse="PassThrough" /> <!-- Required for IIS7 to know to serve up the custom error page -->
</system.webServer>

下面我有一个简单的评论提醒自己:)


随着我越来越深入的研究,我找到了我的具体问题的原因。我走了一条干扰的路线

public class HomeController : BaseController
{
    public ActionResult Pages(string mainCategory, string subCategory, string pageName)
    {
        var model = _pageDetailsRepository.GetPageDetails(mainCategory, subCategory, false);

        if (model == null)
        {
            // return View("Error")
            return HttpNotFound();
        }
    }
}
由于被测试的页面
www.mydomain.com/blahblah
不存在,它进入了页面路径,以检查数据库中是否存在内容,如果没有,则返回一个空模型,该模型反过来返回
视图(“Error”}
,因此没有命中错误控制器

因此,我将BaseController绑定到HomeController,HomeController具有一个
覆盖void ExecuteResult
,以正确捕获404错误

HomeController.cs

protected void Application_Error(object sender, EventArgs e)
{
    var lastError = Server.GetLastError();

    Server.ClearError();

    var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;

    var httpContextWrapper = new HttpContextWrapper(Context);

    var routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Index");
    routeData.Values.Add("statusCode", statusCode);
    routeData.Values.Add("exception", lastError);

    IController errorController = new ErrorController();

    var requestContext = new RequestContext(httpContextWrapper, routeData);

    errorController.Execute(requestContext);

    Response.End();
}
public class ErrorController : Controller
{
    private readonly Common _cf = new Common();
    private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
    private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

    public ActionResult Index(int? statusCode, Exception exception)
    {
        Response.TrySkipIisCustomErrors = true;

        try
        {
            Response.StatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception tryException)
        {
            SendEmail(tryException, statusCode.ToString());

            return Redirect("/");
        }

        SendEmail(exception, Convert.ToString(Response.StatusCode));

        ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";

        ViewBag.ExceptionMessage = Convert.ToString(exception.Message);

        return View("Index");
    }

    private void SendEmail(Exception exception, string errorType)
    {
        const string to = "to@domain.com";
        const string @from = "from@domain.com";

        var subject = "SendEmailError (" + errorType + ")";

        var stringBuilder = new StringBuilder();            
        stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
        stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
        stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
        stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
        stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
        stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
        stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
        stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");

        var body = stringBuilder.ToString();

        _cf.SendEmail(subject, to, from, body, null, true, null, null);
    }
}
Exception ex = Server.GetLastError();
public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea
<customErrors mode="On" defaultRedirect="~/ErrorPage/DisplayError/500">
  <error redirect="~/ErrorPage/DisplayError/403" statusCode="403" />
  <error redirect="~/ErrorPage/DisplayError/404" statusCode="404" />
  <error redirect="~/ErrorPage/DisplayError/500" statusCode="500" />
</customErrors>
public class BaseController : Controller
{
    protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
    {
        return new HttpNotFoundResult(statusDescription);
    }

    protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
    {
        return new HttpUnauthorizedResult(statusDescription);
    }

    protected class HttpNotFoundResult : HttpStatusCodeResult
    {
        public HttpNotFoundResult() : this(null) { }

        public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
    }

    protected class HttpUnauthorizedResult : HttpStatusCodeResult
    {
        public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
    }

    protected class HttpStatusCodeResult : ViewResult
    {
        public int StatusCode { get; private set; }
        public string StatusDescription { get; private set; }

        public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }

        public HttpStatusCodeResult(int statusCode, string statusDescription)
        {
            StatusCode = statusCode;
            StatusDescription = statusDescription;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.HttpContext.Response.StatusCode = StatusCode;

            if (StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = StatusDescription;
            }

            ViewName = "Error";

            ViewBag.Title = context.HttpContext.Response.StatusDescription;

            base.ExecuteResult(context);
        }
    }
}
BaseController


由于VictorySaber,Web.config有
,因为这确保了IIS 7.5通过404头。

我只是在更新这个,但请先尝试注释那一行……好的,通过这些简单的更改,它对您有效吗?谢谢,我已经注释掉了HandleErrorAttribute,在我的webconf中添加了以下内容ig
但仍然没有乐趣,根本没有点击ErrorController。尝试将PassThrough添加到WebConfig也添加了这一点,但不幸的是没有区别,仍然没有点击Error Controller,只是目前正在IIS Express上测试。请您更新您的问题,说明它到底在做什么?它将去哪里?它将如何运行您是否触发了错误?我通常使用错误的url进行测试,例如MyController/FooThank you,您的
建议确实有帮助:-)