繁体   English   中英

ASP.NET MVC和用于不同控制器的不同404页面

[英]ASP.NET MVC and different 404 page for different controller

我想为一个特定的控制器显示一个不同的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));
                    }
                }
            }
        }
     public class ErrorController : Controller {
         publc ActionResult NotFound()
         {
           Return View();
          }
      }

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

在“未找到的操作”中,您可以指定逻辑。 我希望这能帮到您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM