ASP.NET MVC 通配符控制器任意参数路由

ahe*_*ick 3 c# asp.net-mvc asp.net-mvc-routing

我想要控制器/动作,以便当我导航到:

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();
}
Run Code Online (Sandbox Code Playgroud)

我需要设置自定义路由吗?

我试过这个,但它似乎不能处理时期等。

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

Ant*_*t P 5

如果你用一个包罗万象的正则表达式来限制你的路线:

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

并确保您在 web.config 中设置了UrlRoutingModule没有先决条件以确保即使是非托管请求(例如那些被认为具有扩展名的请求)也通过路由模块,您的通用路由应该可以正常工作。