0

I'm using Mvc6 beta6 and model is passed in Json format. What is the recommended way to trim all values inside json model before it is validated? I also need to trim values of strings of nested objects inside the model.

Here is an example:

[HttpPut("{userId}/{id}/details")]    
public async Task<IActionResult> UpdateProfileDetails(string userId, string id, [FromBody] UserProfileDetailsModel userProfileDetailsModel)
{
  // Inside the action all (nested)values of userProfileDetailsModel should be already trimmed without the need to trim them manually based on model type
}

So provided userProfileDetailsModel has a nested object "geoLocation" : { "continent" : " EU "} the effect I'm trying to obtain is having the continent value trimmed as "EU" when reading this value inside the action.

I'm looking for a global filter or global formatter solution which does not depend on model type.

1
  • did you find a way to trim all values? Commented Oct 11, 2016 at 19:44

1 Answer 1

1

Try creating your own model binder Simple ModelBinder

There's a sample available as well at: Model Binding Sample WebSite

You are using [FromBody] which I believe uses input formatters and prevents model binders from running, in that case if you want to go more on the formatter route, have a look at Formatter Sample Web Site

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.