ASP.Net Web API - How can I make it so that prefixes are not required when model binding from the query string? -
in asp.net web api (rc) have test model class so:
[modelbinder] public class testrequest { public string foo { get; set; } public string bar { get; set; } } my controller looks this:
public class testcontroller : apicontroller { public testrequest get(testrequest model) { return model; } } now if invoke action via:
http://.../test?foo=abc&bar=xyz
neither values bind, because model binder expecting model prefixes, such need call:
http://.../test?model.foo=abc&model.bar=xyz
i can understand other action parameters can bind correctly, in case model clean way of encapsulating possible action parameters don't need have nasty action method signature whole lot of optional parameters. allows easy model validation.
is there easy way cause model binding behave same way in mvc, or in post request?
removing modelbinder attribute model class should work in example you've posted. you'll run issues more complex method signatures, see rick strahl's comment: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx#10302750
Comments
Post a Comment