0

I am using web api MVC4. In the get method I will pass the string parameter, which will have space. During execution it gives error.

my WebApiConfig is like below

     config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    config.Routes.MapHttpRoute(
            name: "FindDetailsByCountry",
            routeTemplate: "api/{controller}/FindDetailsByCountry/{country}",
            defaults: new { country = RouteParameter.Optional },
            constraints: new { country = @"^[a-z]+$" }
        );

Now when I run the application

Case 1
input : .../api/contact/
output :
[{"Name":"Ashok","Age":60,"Country":"India"},{"Name":"Nargis","Age":30,"Country":"India"},{"Name":"Nargis","Age":35,"Country":"Iran"},{"Name":"Steve","Age":50,"Country":"South Africa"}]

Case 2
input : .../api/contact/FindDetailsByCountry/india
output :
[{"Name":"Ashok","Age":60,"Country":"India"},{"Name":"Nargis","Age":30,"Country":"India"}]

Case 3
input : .../api/contact/FindDetailsByCountry/South Africa
output : HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable

For case 3, I am getting error. I have checked using these options

  1. ...api/contact/FindDetailsByCountry/South+Africa
  2. ..api/contact/FindDetailsByCountry/South%20Africa
  3. ../api/contact/FindDetailsByCountry/"South Africa"
  4. ../api/contact/FindDetailsByCountry/'South Africa'

Still it is giving the same error.
Actually I will use the output in my android application. In android also I have used URLEncoder.encode("South Africa", "UTF-8"). But no result.

Can any one tell me how to pass the parameter South Africa ?
Thank you so much ps2goat for my previous question. It works now.

Thanks

2 Answers 2

0

As you have constraints which doesn't allow space. Thus you will not be able to pass space./

Your code

constraints: new { country = @"^[a-z]+$" }

Change it to

constraints: new { country = @"^[a-z]\s+$" }
Sign up to request clarification or add additional context in comments.

Comments

0

Look at below link.. it might help..

URL Encoding in c# and Asp.net web api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.