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)
{
    //...
}
6
  • The method in your example is a GET so isn't it implicit that the model will come from the URI anyway? Commented Dec 1, 2015 at 11:20
  • 2
    only in case the model is simple type (int, double, string etc.) Commented Dec 1, 2015 at 11:55
  • looking at asp.net/web-api/overview/formats-and-model-binding/… and stackoverflow.com/a/24629106/1241562 it looks like [FromUri] is implicit when the method is GET (and [FromBody] when POST). Commented Dec 1, 2015 at 12:12
  • No, it isn't the case. See the answer of this topic stackoverflow.com/a/24629106/1241562 Commented Dec 1, 2015 at 12:20
  • Yes, and it doesn't work. Because it tries to bind model from [body], but there is nothing in it. Commented Dec 1, 2015 at 12:39

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

3 Comments

It works only for specified types, but I'd like to apply [FromUriAttribute] to any request model
strathweb.com/2013/04/… 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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.