I am developing a web application using ASP.NET MVC and Web API. I need the following mapping:
http://mysite/ => Welcome page (MVC)
http://mysite/Help => Help page (MVC)
http://mysite/anything_else_here => Web API Search with "anything_else_here" param
I use the following bindings:
// RouteConfig.cs, MVC routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
and
//WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "CatchAll",
routeTemplate: "{q}",
defaults: new { controller = "Search", action = "GetGeneric" },
constraints: new { q = @"\S+" }
This config works fine for all queries EXCEPT for those where there is a percentage mark (used for space encoding).
Eg queries like
http://mysite/shop
http://mysite/get+discount
http://mysite/redeem+code
work fine, but
http://mysite/shopping%20experience
http://mysite/get%20discount
http://mysite/redeem%20code
don't. Instead, I get an error 404 "The resource cannot be found".
Any ideas how I can catch these queries?
The constrain "\\S+" does match the percentage mark.
In my opinion, this is by design. Your contraints are tested, but only after decoding the part of the URL that fits the parameter. Your %20
is actually a space.
Your plus sign is not an encoded character and gets treated literally, so it matches the 'non-whitespace' class of the constraint expression.
Which begs the question: why do you have a constraint on that route?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.