问题描述:

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?

网友答案:

There is no need to read the query string, the WEB API model binder will bind any query string parameters to parameters of your action method... I have never known a scenario where I needed to manually parse the query string

With Attribute Routing:

Attribute routing allows you to specifiy nullable parameters, you will need to enable attribute routing which you can find hundreds of tutorials on.

[Route("Agency/{state?}/{city?}")
public List<AgencyData> Get(string state = null, string city = null)
{
    List<AgencyData> json;
    json = SQLAllAgencyData("");
    return json;
}

This would make the url look like this... http://xxxxxx.com/api/Agency/Texas/Dallas

I am however almost sure your query string syntax would work too however you will need to try that.

Without Attribute Routing:

If you do not want to add attribute routing to Web API you can also add overloaded action methods to the controller..

// /api/Agency
public List<AgencyData> Get()
{
    var json = SQLAllAgencyData("");
    return json;
}

// /api/Agency?state=texas&city=dallas
public List<AgencyData> Get(string state, string city)
{
    // Params will be equal to your values...
    var json = SQLAllAgencyData("");
    return json;
}

EDIT: Turns out there is no need to overload the action method... simply set the parameter defaults to null... (overload seems cleaner though)

// /Agency/ 
// /Agency?state=Texas&city=Dallas
public List<AgencyData> Get(string state = null, string city = null)
{
   // Check for null.. etc.
}

Edit: To make this work I have used the default routing config...

public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
网友答案:

if you want state and city as parameter of method than just add that to parameter of your method and parameters name should match with query string parameter names

   Get(String state, String city)
网友答案:

You can directly accept the query parameters as method parameters and the query string values can be used in your method.

public List<AgencyData> Get(String state, String city)
{
    //var queryValues = Request.RequestUri.ParseQueryString();
    //string filter = queryValues.Get("filter").ToString();

    List<AgencyData> json;
    json = SQLAllAgencyData("");
    return json;
}
相关阅读:
Top