Best way to format query string in asp.net mvc url?

I noticed that if you sent the routevalue query string via asp.net mvc, you will get all the spaces urlencoded in "% 20". What is the best way to override this formatting, since I would like the space to be converted to a + sign?

I thought maybe using a custom Route object or class that comes from IRouteHandler, but would appreciate any advice you might have.

+3
source share
1 answer

You can try writing your own route:

public class CustomRoute : Route { public CustomRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { var path = base.GetVirtualPath(requestContext, values); if (path != null) { path.VirtualPath = path.VirtualPath.Replace("%20", "+"); } return path; } } 

And register it as follows:

 routes.Add( new CustomRoute( "{controller}/{action}/{id}", new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }), new MvcRouteHandler() ) ); 
+3
source

Source: https://habr.com/ru/post/1310388/


All Articles