Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Route attribute to bind query string with web API?

I'm trying to get this to work:

[Route("api/Default")]
public class DefaultController : ApiController
{
    [HttpGet, Route("{name}")]
    public string Get(string name)
    {
        return $"Hello " + name;
    }
}

by calling this http://localhost:55539/api/Default?name=rami but not working, tried this also:http://localhost:55539/api/Default/Hello?name=rami , Also this not working: http://localhost:55539/api/Default/Hello/rami

like image 810
mshwf Avatar asked Feb 13 '17 08:02

mshwf


People also ask

How do I bind data in Web API?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.

What is route attribute in Web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

How do I add routing to Web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.

How do I use FromUri in Web API?

Use [FromUri] attribute to force Web API to get the value of complex type from the query string and [FromBody] attribute to get the value of primitive type from the request body, opposite to the default rules.


2 Answers

Make sure attribute routing is enabled in WebApiConfig.cs

config.MapHttpAttributeroutes();

ApiController actions can have multiple routes assigned to them.

[RoutePrefix("api/Default")]
public class DefaultController : ApiController {

    [HttpGet]
    //GET api/Default
    //GET api/Default?name=John%20Doe
    [Route("")]
    //GET api/Default/John%20Doe
    [Route("{name}")]
    public string Get(string name) {
        return $"Hello " + name;
    }
}

There is also the option of making the parameter optional, which then allow you to call the URL with out the inline parameter and let the routing table use the query string similar to how it is done in convention-based routing.

[RoutePrefix("api/Default")]
public class DefaultController : ApiController {

    [HttpGet]
    //GET api/Default
    //GET api/Default?name=John%20Doe 
    //GET api/Default/John%20Doe
    [Route("{name?}")]
    public string Get(string name = null) {
        return $"Hello " + name;
    }
}
like image 127
Nkosi Avatar answered Nov 08 '22 17:11

Nkosi


In Web API first route template matching happens and then the action selection process.

Your C# should be like this:

public class DefaultController : ApiController
{
    [HttpGet] 
    [Route("api/Default/{name}")]
    public string Get(string name)
    {
        return $"Hello " + name;
    }
}

Now call should be like this:

http://localhost:55539/api/Default/Get?name=rami
like image 20
Naman Upadhyay Avatar answered Nov 08 '22 18:11

Naman Upadhyay