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
  • 56,678
  • 9
  • 81
  • 133
Slinky
  • 5,512
  • 13
  • 71
  • 128
  • 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,451
  • 2
  • 17
  • 21