How to encode a URL using Asp.net?

I have the following aspx link line that I would like to encode:

 Response.Redirect("countriesAttractions.aspx?=");

I tried the following method:

 Response.Redirect(Encoder.UrlPathEncode("countriesAttractions.aspx?="));

This is another method I've tried:

    var encoded = Uri.EscapeUriString("countriesAttractions.aspx?=");
    Response.Redirect(encoded);

Both are redirected to a page without an encoded URL:

http://localhost:52595/countriesAttractions?=

I tried this third method:

 Response.Redirect(Server.UrlEncode("countriesAttractions.aspx?="));

This time the URL itself will be encoded:

http://localhost:52595/countriesAttractions.aspx%3F%3D

However, I get an error from the user interface:

HTTP Error 404.0 Not Found
The resource you are looking for has been removed, had its name changed, or 
is temporarily unavailable.
Most likely causes:
-The directory or file specified does not exist on the Web server.
-The URL contains a typographical error.
-A custom filter or module, such as URLScan, restricts access to the file.

Also, I would like to encode another type of URL that includes parsing session strings:

Response.Redirect("specificServices.aspx?service=" + 
Session["service"].ToString().Trim() + "&price=" + 
Session["price"].ToString().Trim()));

The method with which I tried to include the encoding method in the code above:

Response.Redirect(Server.UrlEncode("specificServices.aspx?service=" + 
Session["service"].ToString().Trim() + "&price=" + 
Session["price"].ToString().Trim()));

The above encoding method that I used displayed the same results that I got using the previous encoding methods of the server url. I am not sure how to encode the URL correctly without getting errors.

, URL- CommandArgument:

Response.Redirect("specificAttractions.aspx?attraction=" + 
e.CommandArgument);

:

Response.Redirect("specificAttractions.aspx?attraction=" + 
HttpUtility.HtmlEncode(Convert.ToString(e.CommandArgument))); 

.

, URL- ? , - , , .

, stackoverflow, , . AntiXSS , , , AntiXSS. URL , , . .

+4
1

UrlEncode UrlPathEncode HttpUtility , . . https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx

, URL. , , URL-, .

, .NET:

string url = "https://www.google.co.uk/search?q=";
//string url = "http://localhost:52595/specificAttractions.aspx?country=";
string parm = "Bora Bora, French Polynesia";
Console.WriteLine(url + parm);
Console.WriteLine(url + HttpUtility.UrlEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(url + HttpUtility.UrlPathEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(HttpUtility.UrlEncode(url + parm), System.Text.Encoding.UTF8);

:

https://www.google.co.uk/search?q=Bora Bora, French Polynesia
https://www.google.co.uk/search?q=Bora+Bora%2c+French+Polynesia
https://www.google.co.uk/search?q=Bora%20Bora,%20French%20Polynesia
https%3a%2f%2fwww.google.co.uk%2fsearch%3fq%3dBora+Bora%2c+French+Polynesia

, , URL, .

(NB , URL- , , - , , # , .)

: https://dotnetfiddle.net/gqFsdK

, , , , . - , (, - ).

N.B. , URL- - URL-. , URL-, . URL- , , .

0

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


All Articles