'. Symbol Breaks Routing
Currently scratching my head as to why routing to one of my functions is failing.
The route is defined via an attribute:
[Route("emails/{emailAddress}")]
[SwaggerResponse(HttpStatusCode.OK, null, typeof(EmailResponse))]
public async Task<IHttpActionResult> GetEmail(string emailAddress)
This works fine if I pass in normal text, i.e. "abc". It hits the route as expected. If I pass in an actual email address: "[email protected]", I get a 404 back and the endpoint is not hit. It seems that the . symbol is breaking the pattern matching and I have no idea why currently.
I've tried a bunch of different things, including adding RouteDebugger, changed the parameter type to object instead of a string, but nothing seems to work and I haven't been able to track down any clues as to why.
Edit:
Seems that enclosing my parameter in / works. So if the URL ends up being
email/[email protected]/ it works
vs
email/[email protected]
which does not work.
Still have no idea what is actually causing this problem though.
Solution 1:[1]
In asp.net core for such things I use asterisk (*
) before the route parameter, by this way you can pass any special character into the route
[Route("emails/{*emailAddress}")] //added *
[SwaggerResponse(HttpStatusCode.OK, null, typeof(EmailResponse))]
public async Task<IHttpActionResult> GetEmail(string emailAddress)
But note when you are using *
it can't possible to add another route parameter after *
for example
This isn't work
[Route("emails/{*emailAddress}/{anther}")]
But this is ok:
[Route("emails/{other}/{*emailAddress}")]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Saeed Esmaeelinejad |