DefaultInlineConstraintResolver Error in WebAPI 2

后端 未结 6 1571
春和景丽
春和景丽 2020-12-23 13:10

I\'m using Web API 2 and I\'m getting the following error when I send a POST to my API method using IIS 7.5 on my local box.

The inline constraint resolver o         


        
相关标签:
6条回答
  • 2020-12-23 13:24

    You get strings as a type in the following manner

    [HttpGet]
    [Route("users/{name}")]
    public User GetUserByName(string name) { ... }
    

    Basically you just don't specify the type

    0 讨论(0)
  • 2020-12-23 13:27

    I got this error when Type as declared as string. When I changed that to int it started working

    [HttpGet][Route("testClass/master/{Type:string}")]
    
    0 讨论(0)
  • 2020-12-23 13:29

    I also got this error when I left a space between the variable name and the variable type in the route, like so:

    [HttpGet]
    [Route("{id: int}", Name = "GetStuff")]
    

    It should be the following:

    [HttpGet]
    [Route("{id:int}", Name = "GetStuff")]
    
    0 讨论(0)
  • 2020-12-23 13:30

    One more thing if you can't use int, bool or any other constraint it is key sensitive and you need to remove any white spaces.

    //this will work
    [Route("goodExample/{number:int}")]
    [Route("goodExampleBool/{isQuestion:bool}")]
    //this won't work
    [Route("badExample/{number : int}")]
    [Route("badExampleBool/{isQuestion : bool}")]
    
    0 讨论(0)
  • 2020-12-23 13:35

    The error means that somewhere in a Route, you specified something like

    [Route("SomeRoute/{someparameter:string}")]
    

    "string" is not needed as it is the assumed type if nothing else is specified.

    As the error indicates, the DefaultInlineConstraintResolver that Web API ships with does not have an inline constraint called string. The default supported ones are the following:

    // Type-specific constraints
    { "bool", typeof(BoolRouteConstraint) },
    { "datetime", typeof(DateTimeRouteConstraint) },
    { "decimal", typeof(DecimalRouteConstraint) },
    { "double", typeof(DoubleRouteConstraint) },
    { "float", typeof(FloatRouteConstraint) },
    { "guid", typeof(GuidRouteConstraint) },
    { "int", typeof(IntRouteConstraint) },
    { "long", typeof(LongRouteConstraint) },
    
    // Length constraints
    { "minlength", typeof(MinLengthRouteConstraint) },
    { "maxlength", typeof(MaxLengthRouteConstraint) },
    { "length", typeof(LengthRouteConstraint) },
    
    // Min/Max value constraints
    { "min", typeof(MinRouteConstraint) },
    { "max", typeof(MaxRouteConstraint) },
    { "range", typeof(RangeRouteConstraint) },
    
    // Regex-based constraints
    { "alpha", typeof(AlphaRouteConstraint) },
    { "regex", typeof(RegexRouteConstraint) }
    
    0 讨论(0)
  • 2020-12-23 13:44

    I designed a API route for one Undo Web API method and I tried to apply ENUM datatype validation on action in route and encountered below DefaultInlineConstrainResolver Error

    Error: System.InvalidOperationException: 'The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'ActionEnum’

    [HttpGet]
    [Route("api/orders/undo/{orderID}/action/{actiontype: OrderCorrectionActionEnum}")]
    public IHttpActionResult Undo(int orderID, OrderCorrectionActionEnum actiontype)
    {
        _route(undo(orderID, action);
    }
    
    public enum OrderCorrectionActionEnum
    {
        [EnumMember]
        Cleared,
    
        [EnumMember]
        Deleted,
    }
    

    To apply ENUM constrain, you have to create custom OrderCorrectionEnumRouteConstraint by using IHttpRouteConstraint.

    public class OrderCorrectionEnumRouteConstraint : IHttpRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // You can also try Enum.IsDefined, but docs say nothing as to
            // is it case sensitive or not.
            var response = Enum.GetNames(typeof(OrderCorrectionActionEnum)).Any(s = > s.ToLowerInvariant() == values[parameterName].ToString().ToLowerInvariant());
            return response;
        }
    
        public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary< string, object> values, HttpRouteDirection routeDirection)
        {
            bool response = Enum.GetNames(typeof(BlockCorrectionActionEnum)).Any(s = > s.ToLowerInvariant() == values[parameterName].ToString().ToLowerInvariant());
            return response;              
        }
    }
    

    Reference (This is my blog): https://rajeevdotnet.blogspot.com/2018/08/web-api-systeminvalidoperationexception.html for more details

    0 讨论(0)
提交回复
热议问题