user895852
user895852

Reputation: 33

asp.net MVC3: Trimming all HTTP POST data

I need to do trim to all HTTP POST data submitted by users through web forms. Having done googling, apparently there is no in-built functionality in asp.net to trim all HTTP POST data.

The closest that I can get is what is described here: ASP.NET MVC: Best way to trim strings after data entry. Should I create a custom model binder?

Unfortunately it doesn't work on nested ViewModels (ViewModel with property with type of other ViewModel).

What is the best way to achieve this? I don't want to do property.Trim() on every properties in all ViewModel. Thank you.

Upvotes: 2

Views: 762

Answers (1)

dahlbyk
dahlbyk

Reputation: 77540

One option is to define your own IValueProvider. I would start by inheriting from NameValueCollectionValueProvider to make a TrimmedNameValueCollectionValueProvider in which you trim the results as you pull them out. Then you would defined a TrimmedFormValueProvider that passes in controllerContext.HttpContext.Request.Form as the collection.

Upvotes: 1

Related Questions