Max
Max

Reputation: 3370

WebAPI: ApiController with a URL string / URI parameter

I'd like to have a ApiController that has an action with a URI or URL parameter.

http://testserver/api/Controller?id=http://www.stackoverflow.com

I implemented actions like:

public bool Get(string id)
{
  ...
}

or

public bool Get(Uri id)
{
  ...
}

However: Whenever I call the URL as stated above, I get an 400 - Error - Bad Request.

Upvotes: 1

Views: 2865

Answers (2)

Max
Max

Reputation: 3370

The problem seems to be that I used "Cassini", the VS integrated webserver. As soon as I ran it in IIS Express, it worked.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

I am unable to reproduce the problem you are describing. This should work. Here are the steps I have tried:

  1. Create a new ASP.NET MVC 4 application using the Empty template
  2. Add an Api controller:

    public class ValuesController : ApiController
    {
        public HttpResponseMessage Get(Uri id)
        {
            return Request.CreateResponse(HttpStatusCode.OK, id);
        }
    }
    
  3. Run the application and navigate to /api/values/?id=http%3A%2F%2Fwww.stackoverflow.com

  4. The id parameter is correctly bound to the Get action argument

So could you provide the steps allowing us to reproduce the problem?

Upvotes: 2

Related Questions