Allows asp.net web APIs to support both [AcceptVerbs ("GET", "POST")] and apiacceptverbs
When using a third-party interface, sometimes we can see that the interface supports both GET and POST. At that time, we thought that webapi has the AcceptVerbs feature and will certainly support it if we don't think about it. It will be used in later projects, at that time, it was supported without passing in parameters until a few days earlier, and the interface for passing in parameters was met.
Still follow the original idea. When I write a parameter, I use FromUri or FromBody. The problem arises, the default webapi does not support both GET and POST [when multiple parameters are required]. Imagine that the web api and asp.net mvc accept parameter binding is different.
Inspired by some searches, I found the following code.
public class FromUriOrBodyParameterBinding : HttpParameterBinding { HttpParameterBinding _defaultUriBinding; HttpParameterBinding _defaultFormatterBinding; public FromUriOrBodyParameterBinding(HttpParameterDescriptor desc) : base(desc) { _defaultUriBinding = new FromUriAttribute().GetBinding(desc); _defaultFormatterBinding = new FromBodyAttribute().GetBinding(desc); } public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext.Request.Content != null && actionContext.Request.Content.Headers.ContentLength > 0) { return _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken); } else { return _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken); } }
config.ParameterBindingRules.Insert(0, x => { if (x.ParameterType == typeof(LoginModel)) { return new FromUriOrBodyParameterBinding(x); } return null; });
Here, it is supported in principle.
However, as a programmer, such writing cannot be tolerated.
The FromBody feature will be introduced later. Therefore, you can view the webapi source code.
Read the source code and follow the FromBody Implementation ideas. The following code is available.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)] public sealed class FromUriOrBodyAttribute : ParameterBindingAttribute { public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter) { return new FromUriOrBodyParameterBinding(parameter); } public class FromUriOrBodyParameterBinding : HttpParameterBinding { HttpParameterBinding _defaultUriBinding; HttpParameterBinding _defaultFormatterBinding; public FromUriOrBodyParameterBinding(HttpParameterDescriptor desc) : base(desc) { _defaultUriBinding = new FromUriAttribute().GetBinding(desc); _defaultFormatterBinding = new FromBodyAttribute().GetBinding(desc); } public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext.Request.Content != null && actionContext.Request.Content.Headers.ContentLength > 0) { return _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken); } else { return _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken); } } } }
The final usage is as follows:
[AcceptVerbs("GET","POST")] public IHttpActionResult Login([FromUriOrBody]LoginModel login) { return Ok(login); }
Here, we only need to record the troubleshooting mark.