0

I'm using MVC4 for the first time and I'm trying to create a WebService. However, when I try this: http://localhost:****/api/mycontroller/?number=1&id=7 I have no way to retrieve the data from the URL.

How can I get those 2 variables? Request.QueryString["ParameterName"] results in an error, it does not recognize this function.

Thanks.

1
  • 2
    add number and id params to your action method definition Commented Mar 21, 2013 at 20:43

1 Answer 1

2

I assume you are referring to the WebApi that allows us to build RESTful applications. If yes, then there is not even a Request object in an ApiController because the System.Web.Mvc is not imported. The way Controller method works in an ApiController is different than MVC Controllers in that api methods are used or called as HTTP methods. So if you have:

[HttpGet]        
public int Count(int id)
{            
    return 50;
}

public string Get(int id)
{
    return "value";
}

That won't work by default, without adding custom routes, because the framework sees both methods as the same. In relation to your question if you want to capture querystring in a GET other than the default Get(int id) you should define them as method parameters as Dave A mentioned, like so:

public string GetByNumberAndId(int number, int id) {
   return "somevalue";
}

And you can call the method just as you are doing it right now:

http://localhost:****/api/mycontroller/?number=1&id=7

You can read more about WebApi on it's official site. This tutorial can give you a good push although it was written a year ago, it's still a good resource to start with.

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

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.