【问题标题】:404 redirect not working with MVC3404 重定向不适用于 MVC3
【发布时间】:2013-09-13 06:29:20
【问题描述】:

我有一个 MVC3 网站,我正在尝试为访问数据库内容的小型网站构建自定义错误页面。

在我的配置中,我得到了:

<system.web>
  <customErrors mode="On">
    <error statusCode="404" redirect="/404" />
    <error statusCode="500" redirect="/error/500.html" />
  </customErrors>

/404 路由在我直接在浏览器中访问时有效。

现在显示标准的“404 页面未找到”。

在我的控制器中,我得到了:

public ActionResult ShowPage(int test)
{
     var page = _rep.GetPage(test);

     if(page == null)
         return HttpNotFound();    
     //....
}

这里可能缺少什么?感觉配置中缺少了一些东西

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    对于自定义错误,我总是将以下方法放在 global.asax 中,这将激活我的错误控制器(基于此 SO 答案 https://stackoverflow.com/a/5229581/426840):

    protected void Application_Error()
            {
                var exception = Server.GetLastError();
                var httpException = exception as HttpException;
    
                // Don't execute error controller for custom error mesages when custom error is disabled
                if (!HttpContext.Current.IsCustomErrorEnabled) return;
    
                Response.Clear();
                Server.ClearError();
    
                Response.StatusCode = 500;
    
                // Avoid IIS messing with custome errors https://stackoverflow.com/a/1719474/426840
                // Also https://stackoverflow.com/a/2345742/426840 for web.config
                Response.TrySkipIisCustomErrors = true;
    
                if (httpException != null)
                {
                    Response.StatusCode = httpException.GetHttpCode();
                }
    
                var routeData = new RouteData();
                routeData.Values["controller"] = "Error";
                routeData.Values["action"] = Response.StatusCode != 404 ? "Index" : "NotFound";
                routeData.Values["exception"] = exception;
                routeData.Values["logId"] = logId;
    
                IController errorsController = new ErrorController();
    
                var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    
                errorsController.Execute(rc);
            }
    

    错误控制器:

    public class ErrorController : AuthorizedController
        {
            public ActionResult Index(string logId)
            {
                if (Request.IsAjaxRequest())
                {
                    return new HttpStatusCodeResult(Response.StatusCode, Response.StatusDescription);
                }
    
                ViewBag.HttpStatusCode = Response.StatusCode;
                ViewBag.HttpStatusMessage = Response.StatusDescription;
                ViewBag.LogId = logId;
    
                return View("Error");
            }
    
            public ActionResult NotFound(string logId)
            {
                if (Request.IsAjaxRequest())
                {
                    return new HttpStatusCodeResult(Response.StatusCode, Response.StatusDescription);
                }
    
                ViewBag.HttpStatusCode = Response.StatusCode;
                ViewBag.HttpStatusMessage = Response.StatusDescription;
                ViewBag.LogId = logId;
    
                return View("NotFound");
            }
        }
    

    Web.config:

    <customErrors mode="On" />
    
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <!-- Avoid IIS messing with custom errors https://stackoverflow.com/a/2345742/426840 -->
        <httpErrors existingResponse="PassThrough" />
    
      </system.webServer>
    

    这适用于 404,但也适用于所有其他错误。

    【讨论】:

      【解决方案2】:

      HttpNotFound 结果返回一个空视图。你需要抛出一个异常

      if(page == null)
          throw new HttpException(404, "Not found")
      

      【讨论】:

      • 我改成了那个,默认的http IIS错误页面显示(和页面上的404状态)。但没有重定向到我的 /404 路线
      猜你喜欢
      • 2021-04-16
      • 2013-04-10
      • 2017-08-28
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 2021-09-25
      • 2012-03-27
      相关资源
      最近更新 更多