编程语言
首页 > 编程语言> > C#-ASP.NET MVC通配符控制器的任何参数路由

C#-ASP.NET MVC通配符控制器的任何参数路由

作者:互联网

我想要控制器/动作,以便在导航至:

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

解决方法:

如果您使用通用正则表达式来限制路线:

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

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

标签:asp-net-mvc-routing,c,asp-net-mvc
来源: https://codeday.me/bug/20191029/1957654.html