2014-01-18 75 views
0

我想爲特定的控制器顯示不同的404頁面,然後使用我現在的默認值。我怎樣才能做到這一點?這裏是我的設置現在:ASP.NET MVC和不同控制器的不同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)); 
        } 
       } 
      } 
     } 
+0

看看這些現有[http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc](http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc)回答 – TugboatCaptain

回答

0
 public class ErrorController : Controller { 
     publc ActionResult NotFound() 
     { 
      Return View(); 
      } 
     } 

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

在NOTFOUND行動,你可以指定你的邏輯。我希望這能幫到您。

+0

我怎麼知道它來自哪裏? –

相關問題