Akbar Badhusha
Akbar Badhusha

Reputation: 2627

1. Forward slash in routing parameter

I have a GET end point which will take user name as parameter. Below is the action

[Route("user/{userName}")]
public User GetUserByName([FromUri] string userName)
{
  // logic here
}

This is how i make the request.

var restClient = new RestClient("uri");
var request = new RestRequest("user/" + userName);
var response = restClient.Execute(request);

It worked fine for all cases till a user with name containing forward slash came. Eg: Akbar/Badhusha Then the request will looks like user/Akbar/Badhusha

This causing the request to return Not Fount error

I tried to add the parameter with AddQueryParameter method. All returning Not found error.

  1. I also tried HttpUtility.UrlEncode
  2. Also tried replacing / with %2f
  3. Also tried user?userName=Akbar/Badhusha

All of them failed.

Is there any way to make it work?

Upvotes: 2

Views: 711

Answers (1)

Dani Mathew
Dani Mathew

Reputation: 828

Try removing [FromUri] from the parameter as shown below,

[Route("user")]
public User GetUserByName(string userName)
{
  // logic here
}

And the request may look like,

user?userName=Akbar/Badhush

Upvotes: 2

Related Questions