gpt4 book ai didi

c# - ASP.NET MVC Wild Card Controller 任意参数路由

转载 作者:行者123 更新时间:2023-11-30 15:28:20 25 4
gpt4 key购买 nike

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

最佳答案

如果您使用包罗万象的正则表达式来限制您的路线:

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

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

关于c# - ASP.NET MVC Wild Card Controller 任意参数路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25940554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com