0

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

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();
        }

    }

提前致谢!

4

1 回答 1

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");
            }
        }
于 2013-08-09T15:33:18.677 回答