Reading a query string in asp.net without specifying the name of any page

How to read any lines on an aspx page.

example: http://foo.com/bike stand

I want to read / get lines on the specified aspx page. expected row row stand for bicycles
expected page getstring.aspx

Here I like to read the line and redirect to the specified page.
Note: I like to do this only in ASP.Net (not with MVC)

+6
source share
3 answers

You can probably solve this using Route . I did a simple demo that you can try in just a couple of minutes. In the Global.asax.cs file, add this method:

 void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("Products", "Products/{product}", "~/getstring.aspx", false, new RouteValueDictionary { { "product", "NoneSelected" } } ); } 

In the same file, in the existing void Application_Start(object sender, EventArgs e) method, add RegisterRoutes(RouteTable.Routes); :

 void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); } 

In doing so, you configured Route , which will accept the request as follows:

 http://foo.com/Products/bike%20stand 

and map it to getstring.aspx . Please note that my url encoded the space in the url.

In getstring.aspx you can access the value ("bike stand") as follows:

 protected void Page_Load(object sender, EventArgs e) { string routeValue = ""; if (RouteData.Values.ContainsKey("product")) routeValue = RouteData.Values["product"].ToString(); //routeValue now contains "bike stand" SelectedProduct.Text = routeValue; } 

I installed Route in this example on the Products path under the application folder. I do not recommend setting up the route directly in the application folder, as in the question. You can do this though if you absolutely want to:

 void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("Products", "{product}", "~/getstring.aspx", false, new RouteValueDictionary { { "product", "NoneSelected" } } ); } 
+4
source

This may not be the best way to meet your needs, but if you want to get part of a string from a URL, you can use URI segments.

 HttpContext.Current.Request.Url.Segments.Select(x=>x.TrimEndβ€Œβ€‹('/')).Skip(2).ToArrβ€Œβ€‹ay(); 

OR

 new Uri("http://www.someurl.com/foo/xyz/index.htm#extra_text").Segments 

Results: [ "/", "foo/", "xyz/", "index.html" ]

After reading the line, you can do whatever you want, such as redirection, etc.

+3
source

You can use the IIS URL Rewrite Module for this . During installation, you can create a rule in the Web.Config System.Webserver node that rewrites the URL so that the string can be processed in the code behind as a QueryString.

 <rewrite> <rules> <rule name="getString" stopProcessing="true"> <match url="^([a-z0-9- /]+)$" ignoreCase="true"/> <action type="Rewrite" url="getstring.aspx?string={R:1}"/> </rule> </rules> </rewrite> 

Then you can just get the string value with this:

 string getString = Request.QueryString["string"]; 

But it will be created by a problem if there are other pages than getstring.aspx than what you need to get, since each request is now sent to getstring.aspx. It might be better to use a prefix so that you can identify the request URL as a string containing a string.

 http://foo.com/getstring/bike stand <match url="^getstring/([a-z0-9- /]+)$" ignoreCase="true"/> 
+2
source

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


All Articles