I have the following enum for Web API 2:
[TypeConverter(typeof(StringEnumConverter))]
public enum SortDirection
{
[EnumMember(Value = "asc")] Ascending,
[EnumMember(Value = "desc")] Descending
}
I'm using the TypeConverterAttribute
with StringEnumConverter
from Newtonsoft.Json
. If I serialize this enum to JSON it ends up looking like I want it:
// "property": "asc"
// "property": "desc"
I'd like to be able to bind this to a model coming from the URI in Web API so that I can pass in ?direction=desc
public class ExampleController : ApiController
{
public IHttpActionResult GetAll([FromUri]SortDirection sortDir)
{
// the above property is not binding to value asc / dir from the query string
// it only binds to 0/1 or Ascending/Descending
}
}
Currently, I can pass in ?direction=0|1
or ?direction=Ascending|Descending
and Web API will bind it.
I would like to be able to do this without referring to SortDirection
explicitly...any enum that I have which uses EnumMemberAttribute
I would like to apply this logic to.
How can I accomplish this?