Allow the ASP. NET Web API to support [Acceptverbs ("GET", "POST")]

Source: Internet
Author: User

When using a third-party interface, you can sometimes see

public class Fromuriorbodyparameterbinding:httpparameterbinding {httpparameterbinding _defaulturibinding;        Httpparameterbinding _defaultformatterbinding; Public fromuriorbodyparameterbinding (Httpparameterdescriptor desc): Base (DESC) {_defaulturi Binding = new Fromuriattribute ().            Getbinding (DESC); _defaultformatterbinding = new Frombodyattribute ().        Getbinding (DESC);  } public override Task Executebindingasync (Modelmetadataprovider metadataprovider, Httpactioncontext Actioncontext, CancellationToken cancellationtoken) {if (actionContext.Request.Content! = null && actionconte Xt. Request.Content.Headers.ContentLength > 0) {//We have something from the body, try that fir             St return _defaultformatterbinding.executebindingasync (Metadataprovider, Actioncontext, CancellationToken); } else {///We needTo read things from Uri return _defaulturibinding.executebindingasync (Metadataprovider, Actioncontext, Canc            Ellationtoken); }        }

  

Config. Parameterbindingrules.insert (0, x =                = {if (X.parametertype = = typeof (Loginmodel))                {                    return new Fromuriorbodyparameterbinding (x);                }                return null;            });

  

[AttributeUsage (AttributeTargets.Class | Attributetargets.parameter, inherited = True, AllowMultiple = false)] public sealed class Fromuriorbodyattribute:para        Meterbindingattribute {public override httpparameterbinding getbinding (httpparameterdescriptor parameter)        {return new fromuriorbodyparameterbinding (parameter); } public class Fromuriorbodyparameterbinding:httpparameterbinding {httpparameterbinding _defau            lturibinding;            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 Actioncont Ext, CancellationToken CancellatiOntoken) {if (actionContext.Request.Content! = null && actionContext.Request.Content.He Aders.                    ContentLength > 0) {//We have something from the body, try that first                Return _defaultformatterbinding.executebindingasync (Metadataprovider, Actioncontext, CancellationToken); } else {//We need to read things from Uri Retur                N _defaulturibinding.executebindingasync (Metadataprovider, Actioncontext, CancellationToken); }            }        }    }

  

[Acceptverbs ("GET", "POST")]        Public Ihttpactionresult Login ([fromuriorbody]loginmodel login)        {            return Ok (login);        }

  

Allow the ASP. NET Web API to support [Acceptverbs ("GET", "POST")]

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.