0

I can always go and do what is mentioned here. Using Enum.TryParse However What I am doing is passing a string to the request. Then created a local variable on my controller of the Enum type so that I can pass the Enum type to my service method to perform task. I feel like if I can directly convert it to the type of Enum on the request's set property, that would be great.

This is what I have currently:

public enum SearchSortType
{
    None,
    Rating,
    Date,
    Etc
}

[DataContract]
public class MyRequest
{
    /// <summary>
    /// The field to order on. 
    /// </summary>
    /// <value>
    /// The order by.
    /// </value>
    [DataMember(Name = "sortOn")]
    public string SortOn { get; set; }
}

what I want is following. I will keep posting String from the request.

[DataContract]
public class MyRequest
{
    /// <summary>
    /// The field to order on.
    /// </summary>
    /// <value>
    /// The order by.
    /// </value>
    [DataMember(Name = "sortOn")]
    public SearchSortType SortOn { get; set; }

}

Is it possible to keep passing string and get it converted on the set property ( or any other way to the enum so that I don't have to create a local variable and convert and then use it?

atiqorin
  • 446
  • 3
  • 13
  • The value passed to `set` must match the property type. However I suppose you could create a `public string SortOnString { get; set; }` which would get and set the `SortOn` property using a string value. Would that work for you? – Rufus L Jan 04 '19 at 20:22
  • Do you need to pass the request as a string? – eVolve Jan 04 '19 at 20:36
  • Yes. I need to pass the request as a string – atiqorin Jan 04 '19 at 20:44

2 Answers2

1

You can write your own model binder that will accept string.

public class EnumModelBinder : DefaultModelBinder
    {
        /// <summary>
        /// Fix for the default model binder's failure to decode enum types when binding to JSON.
        /// </summary>
        protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
            PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
        {
            var propertyType = propertyDescriptor.PropertyType;
            if (propertyType.IsEnum)
            {
                var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                if (null != providerValue)
                {
                    var value = providerValue.RawValue;
                    if (null != value)
                    {
                        var valueType = value.GetType();
                        if (!valueType.IsEnum)
                        {
                            return Enum.ToObject(propertyType, value);
                        }
                    }
                }
            }
            return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
        }
    }

Then simply register it in your Global.asax file.

protected override void OnApplicationStarted()
{
    base.OnApplicationStarted();

    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);

    // Register your new model binder
    ModelBinders.Binders.DefaultBinder = new EnumModelBinder();
}
NaDeR Star
  • 647
  • 1
  • 6
  • 13
1

Enum is a distinct type that consists of a set of named constants called the enumerator list. You can pass the enum in the request and have an enum property on the web api with the same name and .net model binder will automatically bind it to the enum. When the enum is sent in the request it will be send as an Int.

eVolve
  • 1,340
  • 1
  • 11
  • 30