【问题标题】:ASP.NET MVC and different 404 page for different controllerASP.NET MVC 和不同控制器的不同 404 页面
【发布时间】:2014-02-07 23:52:15
【问题描述】:

我想为特定控制器显示不同的 404 页面,然后使用我现在拥有的默认值。我怎样才能做到这一点?这是我现在的设置:

 protected void Application_Error()
        {
            if (!Request.IsLocal)
            {
                var context = new HttpContextWrapper(HttpContext.Current);

                if (!context.Request.IsAjaxRequest())
                {
                    context.Response.ContentType = "text/html";

                    var unhandledException = Server.GetLastError();
                    var httpException = unhandledException as HttpException;
                    if (httpException == null)
                    {
                        var innerException = unhandledException.InnerException;
                        httpException = innerException as HttpException;
                    }

                    Server.ClearError();
                    var routeData = new RouteData();

                    routeData.Values.Add("controller", MVC.Errors.Name);

                    if (httpException != null)
                    {
                        var httpCode = httpException.GetHttpCode();
                        switch (httpCode)
                        {
                            case (int) HttpStatusCode.NotFound:
                                routeData.Values.Add("action", "PageNotFound");

                                IController pageNotFoundController = new ErrorsController();
                                pageNotFoundController.Execute(new RequestContext(context, routeData));
                                break;
                        }
                    }
                    else
                    {
                        routeData.Values.Add("exception", unhandledException);
                        routeData.Values.Add("action", "Error");
                        IController errorController = new ErrorsController();
                        errorController.Execute(new RequestContext(context, routeData));
                    }
                }
            }
        }

【问题讨论】:

标签: c# asp.net asp.net-mvc http-status-code-404


【解决方案1】:
     public class ErrorController : Controller {
         publc ActionResult NotFound()
         {
           Return View();
          }
      }

      <customErrors mode="On">
         <error statusCode="404" redirect="~/Error/NotFound"/>
      </customErrors>

在 NotFound Action 中,您可以指定您的逻辑。我希望这会对你有所帮助。

【讨论】:

  • 我怎么知道它来自哪里?
猜你喜欢
  • 2019-08-15
  • 1970-01-01
  • 2020-07-16
  • 2012-09-24
  • 2018-06-24
  • 2012-05-29
  • 1970-01-01
  • 2011-05-30
  • 2021-09-05
相关资源
最近更新 更多