I am a newbie when it comes to C#, but I need to use it for a project at work. I am building a web page that is using jQuery to call a C# program. The C# program will connect to a SQL server, retrieve data for agencies and return it to the calling webpage as JSON.
I have all that working, I can get both a single agency and a collection of agencies and return it properly. Below is the code:
public class AgencyController : ApiController
{
// GET: api/Agency
public List<AgencyData> Get()
{
//var queryValues = Request.RequestUri.ParseQueryString();
//string filter = queryValues.Get("filter").ToString();
List<AgencyData> json;
json = SQLAllAgencyData("");
return json;
}
// GET: api/Agency/5
public List<AgencyData> Get(string id)
{
List<AgencyData> json;
json = SQLAgencyData(id);
return json;
}
What I want to do now is to be able to pass additional information to the C# program. Something like this: www.domain.com/api/Agency?state=TX&city=Dallas
I can not figure out how to do that. All the examples I found result in build errors. Here are a couple of links I tried:
http://forums.asp.net/t/1072321.aspx?How+to+get+parameter+in+url+by+C+for+net
Is there a way to get all the querystring name/value pairs into a collection?
You can also see the two commented out line in my code, they also don't work. I figure that Request is never set to anything, or defined/declared, but I haven't been able to find an example of how to do that.
Suggestions?
HttpContextwhy can't you access the collection:Request.QueryString["id"];? If it is aPostthen when it hits your controller, name the parameters identical to your query string and Model View Controller for instance, will automatically build and pass the data through to your Controller.