ASP.NET MVC Wild Card Controller任何参数路由

时间:2014-09-19 18:48:49

标签: c# asp.net-mvc asp.net-mvc-routing

我想拥有Controller / Action,以便在我导航到:

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

我是否需要设置自定义路线?

我已经尝试了这个,但它似乎没有处理句号等。

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

1 个答案:

答案 0 :(得分:4)

如果使用全能正则表达式约束路线:

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

并确保您拥有UrlRoutingModule set up in your web.config with no precondition以确保即使是非托管请求(例如那些被视为具有扩展名的请求)通过路由模块,您的捕获路由也可以正常工作。