Formatting Querystring in asp.net MVC 2

Sounds like a question with simple considerations, but I can't figure it out myself ...

I have an actionlink like this

Html.ActionLink( "Test", "test", new { q = "search+twitter" } ) 

This creates a url string as follows

http: // myserver / test? q = search% 2Btwitter

But I would like to keep the plus sign (which I assume is UrlPathEncoded), so that I get the following URL

http: // myserver / test? q = search + twitter

Is there an easy way to do this while continuing to use Html.ActionLink?

+4
source share
2 answers

After a few more searches, I reformulated the question in this post . Using the answer to this question, I was able to put together a suitable routing extension method, which I included below.

 public static class RouteCollectionExtensions { public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults ) { routes.CustomMapRoute( name, url, defaults, null, null ); } public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults, string[] namespaces ) { routes.CustomMapRoute( name, url, defaults, namespaces, null ); } public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults, string[] namespaces, object constraints ) { if ( routes == null ) throw new ArgumentNullException( "routes" ); if ( url == null ) throw new ArgumentNullException( "url" ); var route = new CustomRoute( url, new MvcRouteHandler() ) { Defaults = new RouteValueDictionary( defaults ), Constraints = new RouteValueDictionary( constraints ), DataTokens = new RouteValueDictionary() }; if ( (namespaces != null) && (namespaces.Length > 0) ) { route.DataTokens["Namespaces"] = namespaces; } if ( String.IsNullOrEmpty( name ) ) routes.Add( route ); else routes.Add( name, route ); } } public class CustomRoute : Route { public CustomRoute( string url, IRouteHandler routeHandler ) : base( url, routeHandler ) { } 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; } } 

This extension is then called by the RegisterRoutes method in global.asax so ...

  routes.CustomMapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new string[] { "MyControllers" } ); 
+3
source

A plus sign (after encoding) is a space before encoding.

So, search+twitter will become the “search twitter” in the response message, and to achieve this effect, you can simply use the “search twitter” in the first place:

  Html.ActionLink ("Test", "test", new {q = "search twitter"})

which will generate the next url

http: // myserver / test? q = search% 20twitter

Hope this helps.

0
source

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


All Articles