-1

Possible Duplicate:
How can I properly handle 404 in ASP.NET MVC?

Suppose I have a site www.microsoft.com

And I try to open the url

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

So how to catch these kinds of urls and show a PageNotFound partial view or view

Let me know if you need more detail

Here is my routings:

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" });

getting error:

The controller for path '/anytext' was not found or does not implement 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)
Community
  • 1
  • 1
Bingo
  • 51
  • 2
  • 8

1 Answers1

3

Add a catch all route as the last one in your route collection

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

Edit:

adding a route like this will catch all requests that did not match any previous routes.

Lukas Winzenried
  • 1,919
  • 1
  • 14
  • 22
  • 1
    while this *might* work, it really depends on the rest of his routes and whether they supply defaults or not. if he has the default mvc route, this will only catch things with 4 or more url segments, iirc. – nathan gonzalez Nov 17 '12 at 15:36
  • sorry getting error "The controller for path '/anytext' was not found or does not implement IController." – Bingo Nov 17 '12 at 16:26
  • /anytext matches your default route and ther is no Controller named AnytextController. Thats why you get the exception. Removing the default route would bring the catchall in place. – Lukas Winzenried Nov 17 '12 at 16:43
  • use a tool like http://haacked.com/archive/2011/04/12/routedebugger-2.aspx or http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx to debug your routes. – Lukas Winzenried Nov 17 '12 at 16:43
  • I got answer here: http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc – Bingo Nov 17 '12 at 17:16