简体   繁体   中英

C# MVC Error Handling IIS Express and IIS 7.5

Does a full solution exist to truly handle errors such as 404, 500, etc... on both IIS Express and IIS 7.5?

I have lost count how many articles I have read, regards Web.config, turning customErrors On, Off, etc... or commenting/uncommenting filters.Add(new HandleErrorAttribute()); from the FilterConfig.cs

Can somebody please review what I have so far and tell me what the correct configuration is to enable me to fully trap server errors on both IIS Express and IIS 7.5, display a custom error page and not the Shared/Error.cshtml that seems to get called no matter what and Application_Error is ignored.

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();
}

ErrorController.cs

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);
    }
}

Index.cshtml

@model WebApplication1.Models.Error

@if (HttpContext.Current.IsDebuggingEnabled)
{
    if (Model != null)
    {
        <p><strong>Exception:</strong> @Model.Exception.Message</p>
        <div style="overflow:scroll">
            <pre>@Model.Exception.StackTrace</pre>
        </div>
    }
    else
    {
        <p><strong>Exception:</strong> @ViewBag.ExceptionMessage</p>
    }
}

Error.cs

using System;

namespace WebApplication1.Models
{
    public class Error
    {
        public int HttpStatusCode { get; set; }

        public Exception Exception { get; set; }
    }
}

Any help would be much appreciated :-)

Just had a look through a project I did recently. I only have one line in Application_Error() which is:

Exception ex = Server.GetLastError();

FilterConfig.cs

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.
    }
}

ErrorPageController.cs

public ActionResult DisplayError(int id)
    {
        if (id == 404)
        {
        //... you get the idea

Web.config

<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>

and right down the bottom I have this, with a handy comment to remind myself :)

<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>

As I delved further and further, I figured out the reason for my specific issue. I had a route which was interfering.

routes.MapRoute("Pages", "{mainCategory}/{subCategory}/{pageName}", new { controller = "Home", action = "Pages", subCategory = UrlParameter.Optional, pageName = UrlParameter.Optional, QueryStringValueProvider = UrlParameter.Optional });

As the page tested www.mydomain.com/blahblah didn't exist, it fell into the Pages route to check if the content existed in the database and as it didn't it returned a null model which in turn returned the View("Error"} hence not hitting the Error Controller.

As a result I have bound a BaseController to the HomeController which features a override void ExecuteResult to catch the 404 error correctly.

HomeController.cs

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();
        }
    }
}

BaseController

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);
        }
    }
}

The Web.config has <system.web><customErrors mode="Off" /></system.web> and <system.webServer><httpErrors existingResponse="PassThrough" /></system.webServer> thanks to VictorySaber as this ensures that IIS 7.5 passes the 404 header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM