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")]