MVC3中针对404,500等的漂亮错误URL

时间:2013-08-09 15:24:48

标签: asp.net-mvc-3 web-config http-status-code-404

我当前的错误处理网址看起来很丑:

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

宁可有类似的东西:

http://localhost:65089/Error/NotFound

网络配置代码

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

错误控制器

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

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

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

    }

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以修改Global.asax中的Application_Error方法:

protected void Application_Error(object sender, EventArgs e)
        {   Exception ex = Server.GetLastError();
            if(ex is HttpException && ((HttpException)ex).GetHttpCode()==404)
            {
                Response.Redirect("/Error/NotFound");
            }
        }