0

我有一个 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();    
     //....
}

这里可能缺少什么?感觉就像配置中缺少的东西

4

2 回答 2

2

对于自定义错误,我总是将以下方法放在 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");
        }
    }

网络配置:

<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,但也适用于所有其他错误。

于 2013-09-13T06:59:38.963 回答
1

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

if(page == null)
    throw new HttpException(404, "Not found")
于 2013-09-13T06:31:55.897 回答