Custom Error Messages in ASP.NET MVC

Even when you have custom errors mode on,
and Have all the custom error pages setup, like the following..

    <customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRewrite">
      <error statusCode="403" redirect="~/403" />
      <error statusCode="401" redirect="~/401" />
      <error statusCode="404" redirect="~/404" />
      <error statusCode="409" redirect="~/409" />
      <error statusCode="500" redirect="~/500" />
    </customErrors>


You even set Response.TrySkipIisCustomErrors = true; on your error handler to get rid of those ugly IIS default error pages.
      
        /// <summary>
        ///     Returns a Not Found error.
        /// </summary> 
        [HttpGet, Route("404")]
        [AllowAnonymous]
        public ActionResult NotFound()
        {
            Log.Error("Not Found - 404 : {0}".FormatWith(HttpContext.Request.RawUrl));
            HttpContext.Response.Clear();
            HttpContext.Response.ClearHeaders();
            HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
            HttpContext.Response.TrySkipIisCustomErrors = true;

            return View();
        }

But still, nothing works. Until you set the following config line.


<system.webServer>
    <httpErrors existingResponse="PassThrough"/>
</system.webServer> 

Popular posts from this blog

Print a receipt using a Thermal Printer with C#.NET

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Complex Master-Detail Form using Knockout.js and ASP.NET MVC