Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

DataAnotations with custom ModelBinder not working

$
0
0

I'm having trouble getting my ModelState to validate on a custom object on a ApiController action.  My method is called and the object is bound (using my custom binder) but all of the data anotations that I have on my properties never get validated and the ModelState.IsValid prop is always 'true'.

Trying to accompish via...

- binding from querystring

- http://localhost:1234/api/handler/get

Here's what I have..

//--------- DTO Object

[ModelBinder(typeof(CustomModelBindingProvider))]
    public class MyRequest
    {
        public MyRequest()
        {
        }
        [Required(AllowEmptyStrings=false)]]
        public string AuthenticationToken
        {
            get;
            set;
        }
        [Required(AllowEmptyStrings=false)]
        public string AuthorizationToken
        {
            get;
            set;
        }
    }

/--------------------------------
 //----- Custom BinderProvider

//--------------------------------

    public class CustomModelBindingProvider : ModelBinderProvider
    {
        MyRequestModelBinder _myRequestBinder;

        public CustomModelBindingProvider()
        {
            _myRequestBinder = new MyRequestModelBinder();
        }
        public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
        {
            if (modelType == typeof(MyRequest))
            {
                return _myRequestBinder;
            }
             return null;
        }
    }

// ---------------- Custom Binder

public class MyRequestModelBinder : IModelBinder
    {
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(MyRequest))
            {
                return false;
            }

            MyRequest request = new MyRequest();

            string authToken;
            if (TryGetValue(bindingContext, "autoken", out authToken))
            {
                request.AuthenticationToken = authToken;
            }

            bindingContext.Model = request;
            return true;
        }

        private bool TryGetValue<T>(ModelBindingContext bindingContext, string key, out T result)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(key);
            if (valueProviderResult == null)
            {
                result = default(T);
                return false;
            }
            result = (T)valueProviderResult.ConvertTo(typeof(T));
            return true;
        }
    }


 

 

Everything hooks up correctly but when I actually get into the ApiController method and check the ModelState.IsValid property its always true.  It's almost like the dataanotations handler is never invoked.  Am I supposed to do this manually in my custom binder?

Any Ideas?

Thanks!

Ron

 


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>