'How to do custom model binding for string to enum without comma separation in web api

I want a user to be able to query GET /api/mycontroller?enums=ABC without using commas for the enums parameter. I know I can pass a comma separated parameter but using it without commas returns 'ABC' is not a valid value for type MyEnum. In my database, this field is stored as combination of characters without a comma. Is there a custom model binding attribute I can use and add it to the EnumVal property in MyRequest?

public enum MyEnum 
{
   A=1,
   B=2,
   C=4
}

public class MyRequest
{
   public MyEnum EnumVal {get; set;}
}

[HttpGet("mycontroller")]
public async Task<ActionResult> MyController([FromQuery] MyRequest request)
{
   //query db for row containing resuest.myEnum string combination...
   // ...
}

I've looked into overriding the ValidationAttribute but it still returns an error response.



Solution 1:[1]

Fix the name of the action, since controller is a reserved word, you can not use it for the action name, and add enums input parameter

 public async Task<ActionResult> My([FromQuery] MyRequest request, [FromQuery] string enums)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge