4

I have been pulling my hair out over this all day. I'm trying to just display a friendly cshtml page whenever an exception is thrown so my UX is consistent - I don't want my users even knowing I'm on the .net stack from the UI, ever.

I'm testing by navigating to localhost:2922/junkurl, - if the URL does not resolve, cannot be found, or otherwise generates an exception, I want to display a friendly rendered cshtml page.

What I have in my web.config:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Shared/Error.cshtml">
</customErrors>

This results in the default yellow error page. But if I drop an error.html page in the root and use this:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/error.html">
</customErrors>

It works. The only problem is, I don't want to have to build up my entire Layout / LoginPartial / etc all over again with straight html - I want to render it using razor. What is the typical approach around this? I've done tons of searching on this so apologies if I missed the answer, I'm just completely at a loss.

I would rather do this from code if possible, but I from what I understand, code will only cover a certain level of exceptions... at a certain point it seems it has to be handled via config. I just wish it was straightforward config!

SB2055
  • 12,272
  • 32
  • 97
  • 202

1 Answers1

13

Try with an ErrorController and the following config in your web.config

web.config

<customErrors mode="On" defaultRedirect="~/Error">
  <error redirect="~/Error/NotFound" statusCode="404" />
  <error redirect="~/Error/InternalServer" statusCode="500" />
</customErrors>

ErrorController

public class ErrorController : Controller
{
    public ActionResult Index()
    {
        return View("Error");
    }

    public ActionResult NotFound()
    {
        Response.StatusCode = 200;
        return View("NotFound");
    }

    public ActionResult InternalServer()
    {
        Response.StatusCode = 200;
        return View("InternalServer");
    }
}
Sam
  • 2,917
  • 1
  • 15
  • 28
  • Is there any way to use MVC views/rendering and remove the aspxerrorpath from the URL? Otherwise, this is working great :) – SB2055 Dec 12 '14 at 04:49
  • Reason I ask is because ideally my users won't know that I'm even on .NET... trying to hide as much implementation/architecture as possible – SB2055 Dec 12 '14 at 04:49
  • Add redirectMode="ResponseRewrite" to the customErrors section – Sam Dec 12 '14 at 04:54
  • 2
    If I do this, I get the generic yellow error page again because apparently ResponseRewrite doesn't like MVC routing – SB2055 Dec 12 '14 at 04:55
  • Yeah, my bad. Have a look at this. there's a discussion about your issue here http://stackoverflow.com/questions/2971864/asp-net-mvc-error-handling-remove-aspxerrorpath – Sam Dec 12 '14 at 05:04
  • Read CallMeLaNN's answer – Sam Dec 12 '14 at 05:05
  • While in most scenarios this will work, it might not work when there is a problem in,for example, the web.cofig, or a database mismatch (anything outside the MVC pipeline). Which can cause a redirect loop. a static html file might be a better option. – CularBytes Feb 25 '17 at 15:41