1

I have developed web service using ASP.NET Web API for my application. Find code to access data's from WEP API

namespace ProjectTrackingServices.Controllers
{
   [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class PTEmployeesController : ApiController
    {
 [Route("api/ptemployees/{name:alpha}")]
        public HttpResponseMessage Get(string name)
        {
            var employees = EmployeesRepository.SearchEmployeesByName(name);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, employees);
            return response;
        }
    }
}
  1. I can filter specific employee data's using their name , which has without space in name string. ex: URL: http://localhost:55487/api/ptemployees/john

  2. When i try to filter the employees with full name[ has spaces] by url ex: Url: " http://localhost:55487/api/ptemployees/john markus " Error will return "NetworkError: 400 Bad Request - http://localhost:55487/api/ptemployees/john%20m"

So anyone help how to pass url parameter with spaces and return success status in WEP API.

3
  • 1
    in API mapping you have mention {name:alpha} so alpha does not contain space so please change in URL mapping then try it Commented May 25, 2015 at 7:17
  • Dharmesh, Yes. when change the url mapping it's working fine Commented May 25, 2015 at 7:26
  • @Dharmesh whether you think your comment it's easy or obvious, you should include an answer with taht information to make it clear that this Q has an A, so that's useful for other people Commented May 25, 2015 at 8:05

2 Answers 2

5

The problem is the attribute constriant in the route:

[Route("api/ptemployees/{name:alpha}")]

The alpha constraint requries that all the characters are alphabetic characters, and that doesn't include spaces. You need to remove or change your constraint (If you want to change it, the easiest way is to use a regex constraint).

From the documentation on attribute routing:

alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z)

Sign up to request clarification or add additional context in comments.

Comments

2

The person calling your API should make sure they encode the URL parameters as per the http specification. If the caller if using .net all url params should be passed through the HttpUtility.UrlEncode() static method. In the above case this will turn the url into http://localhost:55487/api/ptemployees/john%20markus

When calling the API, because it now conforms to the HTTP protocol, WEB API will know how to decode this with the proper spaces.

1 Comment

The answer was already in a comment. I asked the one who made the comment to covnert it into an answer, but, as he didn't I've done it myself. The URL encoding wasn't the problem in this occasion.

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.