0
votes

I am not entirely sure about the title of my question. What I need is to set up custom error pages for common errors - 404, 500, etc. but I also need my own "special" error pages for particular errors.

I configured custom error pages served by IIS like below and it is working fine. Something happens, error 500 is raised and /Error/InternalServerError is served and original URL is kept.

But if I access /Error/Special I have problem. If I set status code to 500, I will get content of error page /Error/InternalServerError instead of content of /Error/Special with status code 500. TrySkipIisCustomErrors = true is not working because of web.config.

I have this in my web.config:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <clear/>
  <error statusCode="500" path="/Error/InternalServerError" responseMode="ExecuteURL" />
</httpErrors>

And this controller:

public class ErrorController : Controller
{
    // special error page
    public ActionResult Special()
    {
        //Response.StatusCode = (int)HttpStatusCode.InternalServerError; // <- I cant set status code because IIS returns error page (custom error page) instead
        //Response.TrySkipIisCustomErrors = true; // <- needs httpErrors - existingResponse="Passthrough" but custom error pages are not working with it
        return View();
    }

    public ActionResult InternalServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View();
    }
}

Why I need "special" error pages? I want to do something like this:

public class SpecialController : Controller
{      
    public SpecialController()
    {
        // something
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if ( something != true )
        {
            // it would be nice to do it without redirect, just return /Error/Special and stop executing original request
            filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary
                        {
                            { "action", "Special" },
                            { "controller", "Error" }
                        });
        }
    }
}

Am I missing something?

1

1 Answers

0
votes

I think this blog could helps you. It did it for me years ago.

http://benfoster.io/blog/aspnet-mvc-custom-error-pages