c# - How do I Trim All incoming Json object fields in MVC WEB API? -
what i'm trying achieve trim incoming object properties of type string in mvc web api project.
i thought model binder should solution, it's not being hit, if try set custom model binder instead of modelbinders.binders.defaultbinder
..
the json example {"name": " test name ", "number": 15}
for example - if specify modelbinder
explicitly, works..
[modelbinder(typeof(mycustombinder))] public class testobject { public string name { get; set; } public int number { get; set; } }
the controller ...
public class testcontroller : apicontroller { // post api/test public void post([modelbinder(typeof(mycustombinder))]testobject value) { }
and registration
modelbinders.binders.add(new keyvaluepair<type, imodelbinder>(typeof(testobject), new mycustombinder()));
but want find more generalized approach, wouldn't need decorate each , every model class in project
i believe if your client-side code sends " test name "
whitespaces, user might want way. however, if attending prevent him doing so, in hard way, not in handling <input type='text' .. />
on frontend, might end accessing form
collection of request.
public viewresult index() { request.form["name"] = request.form["name"].trim(); return view(); }
or, in several other ways:
- you make common asbtract controller class, same thing in
onactionexecuting
method. - you define
actionfilter
attribute class, whatever method or controller class mark it.
since working in limited edition of controller, can still access request
property so:
httprequestbase request = ((httpcontextwrapper)request.properties["ms_httpcontext"]).request;
Comments
Post a Comment