aherrick
aherrick

Reputation: 20179

ASP.NET MVC Wild Card Controller any parameter routing

I want to have Controller/Action so that when I navigate to:

mysite.com/whatever. i type here will pipe into...a ! string.

public ActionResult Index(string anything)
{
    // anything = whatever. i type here will pipe into...a ! string.
    return View();
}

Do I need to setup a custom route?

I've tried this but it doesn't seem to handle periods, etc.

        routes.MapRoute(
            name: "Default",
            url: "{*anything}",
            defaults: new { controller = "Home", action = "Index" }
        );

Upvotes: 3

Views: 2871

Answers (1)

Ant P
Ant P

Reputation: 25231

If you constrain your route with a catch-all regex:

routes.MapRoute(
    "Default",
    "{*anything}",
    new { controller = "Home", action = "Index" },
    new { anything = @"^(.*)?$" }
);

And ensure you have the UrlRoutingModule set up in your web.config with no precondition to ensure that even unmanaged requests (e.g. those deemed to have extensions) are put through the routing module, your catchall route should work fine.

Upvotes: 6

Related Questions