让asp.net web api同时支持[AcceptVerbs("GET","POST")]

时间:2015-10-27 17:39:31   收藏:0   阅读:938

   在使用第三方接口时,有时候会看到

 

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)
            {
                // we have something from the body, try that first
                return _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
            }
            else
            {
                // we need to read things from uri
                return _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
            }
        }

  

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 : 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)
                {
                    // we have something from the body, try that first
                    return _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
                }
                else
                {
                    // we need to read things from uri
                    return _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
                }
            }

        }
    }

  

[AcceptVerbs("GET","POST")]
        public IHttpActionResult Login([FromUriOrBody]LoginModel login)
        {
            return Ok(login);
        }

  

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!