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)
{
//...
}
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)
{
//...
}
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.
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());