2012-11-17 76 views
-1

Possible Duplicate:
How can I properly handle 404 in ASP.NET MVC?MVC3路由任何URL

假設我有一個網站www.microsoft.com

我嘗試打開URL

www.microsoft.com/anytext 
www.microsoft.com/anytext/anynumber 
www.microsoft.com/anytext/anynumber/anything 

那麼如何抓住這些類型的網址,然後顯示PageNotFound局部視圖或視圖

讓我知道你是否需要更多細節

這裏是我的路線:

routes.MapRoute("PrivacyAction", "Privacy", new { controller = "Home", action = "Privacy" }); 
routes.MapRoute("LegalAction", "Legal", new { controller = "Home", action = "Legal" }); 
routes.MapRoute("AboutUs", "AboutUs", new { controller = "Home", action = "AboutUs" }); 
routes.MapRoute("Advertising", "Advertising", new { controller = "Home", action = "Advertising" }); 
routes.MapRoute("Contact", "Contact", new { controller = "Home", action = "Contact" }); 
routes.MapRoute("Reputation", "Reputation", new { controller = "Home", action = "Reputation" }); 

routes.MapRoute(
    "Users", // Route name 
    "{controller}/{action}/{id}/{slug}", // URL with parameters 
    new { controller = "User", action = "List", id = UrlParameter.Optional, slug = UrlParameter.Optional }); // Parameter defaults 

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 

routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "404" }); 

收到錯誤:

控制器的路徑 '/ anytext' 未找到或沒有實現一個IController。

堆棧跟蹤:

at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) 
    at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) 
    at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2() 
    at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() 
    at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) 
    at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) 
    at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func) 
    at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) 
    at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) 
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
+0

你可以發佈你定義的路線嗎? –

回答

3

添加一個catch作爲最後一個全部線路的路由集合

routes.MapRoute(
    "Catchall", // Route name 
    "{*url}", // URL with parameters 
    new { controller = "YourController", action = "YourAction" }, 
    null, 
    new[] { "YourNamespace" }); 

編輯在:

添加路由這樣會抓住一切請求不符合任何以前的路線。

+0

雖然這*可能*工作,它真的取決於他的路線的其餘部分,以及他們是否提供默認值。如果他有默認的mvc路由,這隻會捕捉4個或更多url段,iirc。 –

+0

抱歉出現錯誤「未找到路徑'/ anytext'的控制器或未實施IController。」 – Bingo

+0

/anytext匹配您的默認路由,並且它不是名爲AnytextController的控制器。這就是爲什麼你會得到例外。刪除默認路由會帶來滿足感。 –