Processing ASP.NET MVC Controller Parameters

In my application, I have a string parameter called a "store" that is required in all controllers, but needs to be converted using the following code:

shop = shop.Replace("-", " ").ToLower(); 

How can I do this globally for all controllers without repeating this line again and again? Thanks, Leo

+2
source share
1 answer

Write a custom action filter , override OnActionExecuting() and apply the filter to all your controllers. (Or just override OnActionExecuting() in your base controller, if you have a base controller at all.) The action method will look something like this:

 protected override void OnActionExecuting(ActionExecutingContext filterContext) { var parameters = filterContext.ActionParameters; object shop; if (parameters.TryGetValue("shop", out shop)) { parameters["shop"] = ((string)shop).Replace("-", " ").ToLower(); } } 
+3
source

All Articles