Make asp.net web APIs support both [AcceptVerbs (& quot; GET & quot;, & quot; POST & quot;)] and apiacceptverbs

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.