Reputation: 626
I'm having trouble with the ASP.NET Web API routing. Specifically I want to pass a string parameter that is either empty or containing a slash within the Uri. But unfortunately this breaks the routing.
I already have a proper route set up, which is matching fine if I have a normal string such as "fubar". I tried to pass the string url encoded, but this does not work. Apparently the problem is that some http sys layer is already decoding the string, meaning not "fu%2fbar"
arrives, but "fo/bar"
- which leads to another route.
Also when I pass an empty string the uri is "api//..."
, the two /
are merged and the segment is thrown away.
One method to solve the issue with the /
inside is to double encode the Uri and decode it inside my method. But this seems like a very very bad option, so I'm looking for other methods on how I could solve this problem.
Upvotes: 2
Views: 4576
Reputation: 4063
I'd suggest either:
changing the request type to be POST and put the string into the body of the request
or add an interceptor (DelegatingHandler) which will grab the request and rewrite the param in the url, e.g. by replacing the invalid characters
Upvotes: 0
Reputation: 81
For slashes you could prepare special routes
"{controller}/{action}/{param1}/{param2}/{param3}"
"{controller}/{action}/{param1}/{param2}"
"{controller}/{action}/{param1}"
public ActionResult Index(string param1, string param2, string param3)
{
string param = string.Concat(param1, param2, param3);
this way, fu and bar are two params.
Don't have any solution for empty strings. I would replace it in client app by space char or something else which means "empty" but is not
Upvotes: 1