【发布时间】:2015-03-29 16:09:29
【问题描述】:
我正在使用 ASP.NET MVC 和 Web API 开发一个 Web 应用程序。 我需要以下映射:
http://mysite/ => Welcome page (MVC)
http://mysite/Help => Help page (MVC)
http://mysite/anything_else_here => Web API Search with "anything_else_here" param
我使用以下绑定:
// RouteConfig.cs, MVC routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
和
//WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "CatchAll",
routeTemplate: "{q}",
defaults: new { controller = "Search", action = "GetGeneric" },
constraints: new { q = @"\S+" }
此配置适用于所有查询,除了那些有百分比标记的查询(用于空间编码)。
例如像
这样的查询http://mysite/shop
http://mysite/get+discount
http://mysite/redeem+code
工作正常,但是
http://mysite/shopping%20experience
http://mysite/get%20discount
http://mysite/redeem%20code
不要。相反,我收到错误 404“找不到资源”。
有什么办法可以捕捉到这些查询吗?
约束“\S+”与百分比标记匹配。
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-mvc-routing