Where are the trim () parameters of the incoming request?

I have the following code code

public ActionResult Tabs(SearchViewModel svm) { if (Request.IsAjaxRequest()) { svm.Summary = _entitySearchService.GetSearchDataSummary(svm.Search); return PartialView(svm); } else { return RedirectToAction("QuickSearch", "Search" , new RouteValueDictionary { { "search", svm.Search } }); } } 

if the user submits a search that ends with a space, for example. "something", it works fine if it is an ajax request, but if it is not an ajax request, the request is redirected to another action method, after which something goes wrong and 404 is returned.

I could do trim() in an else clause, for example.

new RouteValueDictionary { { "search", svm.Search.Trim() } }

but there are several places that this happens. Ideally, I could do it all in one place.

Would it be considered too hacky if I put it in the Controller Initialize method?

  protected override void Initialize(RequestContext requestContext) { // do a test to see if there a 'search' parameter in requestContext, // and if so, trim it base.Initialize(requestContext); } 

Or is there another better way?

+4
source share
2 answers

You can override the installer of your SearchViewModel if this is an option

 public class SearchViewModel { ... private string search; public string Search { get { return search; } set { search = value.Trim(); } } ... } 
0
source

All Articles