0

My current error handling URLs look rather ugly:

http://localhost:65089/Error/NotFound?aspxerrorpath=/Foo

Would rather have something like this:

http://localhost:65089/Error/NotFound

Web Config Code

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error/Unknown">
      <error statusCode="404" redirect="~/Error/NotFound" />
    </customErrors>

Error Controller

  public class ErrorController : Controller
    {
        //
        // GET: /Error/

        public ActionResult Unknown()
        {
            return View();
        }

        public ActionResult NotFound()
        {
            return View();
        }

    }

Thanks in advance!

pnuts
  • 58,317
  • 11
  • 87
  • 139
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • This may be of some interest: http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc/2577095#2577095 – Tom Chantler Aug 09 '13 at 15:52

1 Answers1

0

You can modify Application_Error method in your Global.asax:

protected void Application_Error(object sender, EventArgs e)
        {   Exception ex = Server.GetLastError();
            if(ex is HttpException && ((HttpException)ex).GetHttpCode()==404)
            {
                Response.Redirect("/Error/NotFound");
            }
        }
Andrey Gubal
  • 3,481
  • 2
  • 18
  • 21