假设我有一个网站www.microsoft.com
我尝试打开网址
www.microsoft.com/anytext
www.microsoft.com/anytext/anynumber
www.microsoft.com/anytext/anynumber/anything
那么如何捕获这些类型的URL并显示一个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。
Stack Track:
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)
添加捕获所有路由作为路由集合中的最后一个路径
routes.MapRoute(
"Catchall", // Route name
"{*url}", // URL with parameters
new { controller = "YourController", action = "YourAction" },
null,
new[] { "YourNamespace" });
修改强>
添加这样的路线将捕获所有与先前路线不匹配的请求。