This project is read-only.

Is [FromUri] needed?

Topics: ASP.NET Web API
Jan 23, 2014 at 4:08 AM
I have a web API Get action that uses a class to capture parameters:
public class MyController : ApiController
    public IHttpActionResult GetOne([FromUri] OneRequest request)
        return Ok(new OneResponse());
I have to use [FromUri] for the parameter because if I don't, web API will look for parameters in the body of the request. I thought HTTP get is supposed to not have a body?

I think that the attribute [HttpGet] is enough to indicate to web API to look for parameters in query string.

What do you think?
Jan 23, 2014 at 3:59 PM

What you have here is fine. The effect of FromUri attribute on complex type causes the values from RouteData & Query String to bound to public properties on the complex type.

Are you seeing anything different? If so, please share how your complex type and request url looks like.

Jan 24, 2014 at 7:30 AM
@kichalla yes, the code works. But my intent is to suggest that web API reads the query string by default if the HTTP method is Get. I was arguing that the user should not need to add the [FromUri] attribute.
Jan 24, 2014 at 5:31 PM
We considered special casing Get to make this work a while back, but we decided it was easier to understand if we have consistent parameter binding rules.

Daniel Roth
Feb 23, 2014 at 5:02 PM
Edited Feb 23, 2014 at 5:03 PM
Filip W posted a great article on query string parameter binding on his blog:

@endy_c: the section "Defaulting to [FromUri] for GET and HEAD requests" should be of particular interest to you. He shows how to do exactly what you're asking by creating a custom DefaultActionValueBinder.