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)
你可以發佈你定義的路線嗎? –