Asp.net webapi enum parameter with default value

前端 未结 3 1182
你的背包
你的背包 2021-02-14 02:44

I have a controller

   [HttpGet]
    [RoutePrefix(\"api/products/{productId}\")] 
    public HttpResponseMessage Products(int productId,TypeEnum ptype=TypeEnum.C         


        
3条回答
  •  梦谈多话
    2021-02-14 03:48

    This type of validation should be handled in pipeline not in controller.

        public abstract class ETagMatchAttribute : ParameterBindingAttribute
    {
        private ETagMatch _match;
    
        public ETagMatchAttribute(ETagMatch match)
        {
            _match = match;
        }
    
        public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
        {
            if (parameter.ParameterType == typeof(ETag))
            {
                return new ETagParameterBinding(parameter, _match);
            }
            return parameter.BindAsError("Wrong parameter type");
        }
    }
    

    something like this. refer to MSDN link for detailed explanation

提交回复
热议问题