0

All my requestModels are passed via Uri.

Is there any chance to use [FromUriAttribute] implicitly instead of applying them to each request model?

[HttpGet]
public IHttpActionResult Test([FromUri]MyClass requestModel)
{
    //...
}
BIKTOP
  • 123
  • 1
  • 2
  • 7
  • The method in your example is a GET so isn't it implicit that the model will come from the URI anyway? – R Day Dec 01 '15 at 11:20
  • 2
    only in case the model is simple type (int, double, string etc.) – BIKTOP Dec 01 '15 at 11:55
  • looking at http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api and http://stackoverflow.com/a/24629106/1241562 it looks like `[FromUri]` is implicit when the method is `GET` (and `[FromBody]` when `POST`). – R Day Dec 01 '15 at 12:12
  • No, it isn't the case. See the answer of this topic stackoverflow.com/a/24629106/1241562 – BIKTOP Dec 01 '15 at 12:20
  • Yes, and it doesn't work. Because it tries to bind model from [body], but there is nothing in it. – BIKTOP Dec 01 '15 at 12:39
  • You're absolutely right, I just set up some test examples and it didn't work. Bill's answer below does what you ask. – R Day Dec 01 '15 at 13:31

1 Answers1

1

If you are not using self hosting, then you can add a parameter binding rule to the WebApiConfig.Register class

config.ParameterBindingRules.Insert(0,typeof(MyClass),x=>x.BindWithAttribute(new FromUriAttribute()));

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

---edited All parts of the web api 2 framework are extendable with some exceptions. In this case it is possible to modify the default behavior of how the web api 2 attempts to bind complex type parameters for all get requests by overriding the behavior of the DefaultActionValueProvider and replacing the default implementation with a custom one. There is a good example of how this may be accomplished at the following link.

http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/

Specifically this is a two step process:

derive a new class the extends the DefaultValueProvider

replace the existing implementation in the services collection

From the URI above this is done in the section "Defaulting to [FromUri] for GET and HEAD requests"

1-Extending the implementation

public class CustomActionValueBinder : DefaultActionValueBinder
{
protected override HttpParameterBinding GetParameterBinding(HttpParameterDescriptor parameter)
{
    return parameter.ActionDescriptor.SupportedHttpMethods.Contains(HttpMethod.Get) || parameter.ActionDescriptor.SupportedHttpMethods.Contains(HttpMethod.Head) ?
               parameter.BindWithAttribute(new FromUriAttribute()) : base.GetParameterBinding(parameter);   
}}

Then replacing then new default implementation in the config.services collection

 config.Services.Replace(typeof(IActionValueBinder), new CustomActionValueBinder());
Bill
  • 1,241
  • 17
  • 25
  • It works only for specified types, but I'd like to apply [FromUriAttribute] to any request model – BIKTOP Dec 01 '15 at 13:30
  • 2
    http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/ The link in the comment describes what I believe would be the implementation of the behavior that you are looking for. There is an extension of the defaultactionvalueprovider followed by the instructions on replacing the default implementation with the custom implementation in the config.services collection. – Bill Dec 01 '15 at 14:24
  • @Bill it would be good if you update relevant part of the article in your answer and also include the link as reference in the answer. – Guanxi Dec 01 '15 at 21:59