Best practice for passing enum params in Web API

前端 未结 4 1847
北恋
北恋 2021-01-07 16:31

I have a RESTful Web API project, and I have 2 different Enum scenarios that I\'m unsure of re best practice.

Scenario 1 : Straightforward Enum Param

4条回答
  •  天涯浪人
    2021-01-07 17:11

    The simplest answer is, "It doesn't matter".

    If the parameter in your controller method is of the enumeration type

    public IHttpActionResult Foo(RuleType ruleType)
    

    In WebAPI, It Just Works - no matter if the client request URL specifies the parmeter value as ?ruleType=1 or ?ruleType=EmailAddress

    If the client specifies a value that isn't valid for the enumeration, an exception is thrown (The parameters dictionary contains a null entry for parameter 'ruleType' of non-nullable type 'RuleType' for method 'Foo' ... and the client gets a 400 Bad Request response.

提交回复
热议问题