Best practice for passing enum params in Web API

前端 未结 4 1859
北恋
北恋 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:30

    Multiple values or not, if you are using an Enum as a string, you have to parse it manually. In .NET the Enums are integers so if you want to send an enum to the default model binder you have to do it like this: ?ruleType=1.

    You can write your own model binder that will accept string, but you have to ask yourself why are we doing it? If you want the user to be able to easily identify the url then use strings. If not there is no reason not to use integers. As you said, you can use FlagsAttribute to combine multiple values.

提交回复
热议问题