Reputation: 595
I need to be able to handle special characters in a REST
call. Specifically the .
and /
characters.
For example I have a GET
route /api/division/{someDivision}
. Now, calling this route with a parameter of /api/division/West Canada/
I get a return and everything works as expected. However, I need to be able to support other business divisions which have names such as "Southwest U.S." and "North/South America". Passing these parameters through my route returns a 404
via the api, since I presume, that the http handler thinks that the .
and /
characters make it think I'm referring to another domain or directory. Is there anyway to work around this so I can pass the needed parameter?
The route:
[HttpGet]
[Route("{division}/information")]
public IHttpActionResult DivisionInfo(string division)
{
...omitted for brevity
Upvotes: 1
Views: 9625
Reputation: 696
Using an arbitrary string as the last URL Path does not work, because ASP.NET Web API interprets a .
in the last URL Parameter as a File-Extension, which usually results in a 404 Not found
.
Even including an expected .
in the Route does not work either e.g. with File Names /api/division/{name}.{extension}
, because default Routing takes precedence.
Appending a unique fixed Literal helps (unless someDivision contains a Slash!):
/api/division/{someDivision}/data
The Morale is: don't use arbitrary strings in the Path. Check the Documentation ASP.NET Web API Routing
Upvotes: 0
Reputation: 1
try adding [FromUri]
before the param:
Route("api/Person/{ID}/[FromUri]{UserName}")]
Upvotes: 0
Reputation: 2476
You could try setting your route up like this:
[HttpGet]
[Route("/api/information")]
public IHttpActionResult DivisionInfo(string division)
Then you can call GET http://website.com/api/information?division=text.with/special./characters
.
Upvotes: 1