Using Request.QueryString, slash (/) is added to the last query line when it exists in the first query line

This is my first post on stackoverlow, and I could not find a solution to this in any other posts, so here it is:

I have a webpage that sends two lines of request to a URL:

example.aspx?name=<%=name%>&sku=<%=sku%>

Then I collect the values ​​with Request.QueryString["name"];andRequest.QueryString["sku"];

When I look at the source URL of the page sending the query strings, everything looks fine, but if the “name” contains a slash (/), it will somehow snap to the end of the “sku” when I get the value of the query string. I tried replacing / with% 2F, but this does not work. If the query string "name" does not have a slash, everything looks right.

Any ideas?

Edit: I had to double the encoding (server.urlencode) and perform double decoding to make it work correctly. Thank you for your help!

+3
source share
3 answers

In fact, you should encode your values ​​for URLs using the HttpServerUtility.UrlEncode method :

example.aspx?name=<%=Server.UrlEncode(name)%>&sku=<%=Server.UrlEncode(sku)%>

URL encoding ensures that all browsers will correctly pass text to the URL of the string. Characters such as question mark (?), Ampersand (&), label (/), and spaces may be truncated or damaged by some browsers. As a result, these characters must be encoded in the tags or in the query string, in which the string can be re-sent by the browser in the query string.

EDIT:

, : name = Bellagio™ 16 1/2" High Downbridge Outdoor Wall Light sku = 46910: -, :

public string Name
{
    get
    {
        return "Bellagio™ 16 1/2\" High Downbridge Outdoor Wall Light";
    }
}

public string Sku
{
    get
    {
        return "46910";
    }
}

:

<a href='1.aspx?name=<%=Server.UrlEncode(Name)%>&sku=<%=Server.UrlEncode(Sku)%>'>
    this is a link
</a>

( ):

protected void Page_Load(object sender, EventArgs e)
{
    var name = Request.QueryString["name"];
    var sku = Request.QueryString["sku"];
}

, : Bellagio™ 16 1/2\" High Downbridge Outdoor Wall Light 46910.

, URL, : LifeSizePDF.aspx?productname=Bellagio&amp;%238482%3b+16+1%2f2&amp;quot%3­b+High+Downbridge+Outdoor+Wall+Light&amp;shortsku=46910%2f

+4

URL , , .

, , , HttpUtility , Page, Server:

var encodedValue = HttpUtility.UrlEncode(rawValue);

var encodedValue = Server.UrlEncode(rawValue);
+2

You can just trim the final value of the sku request:

Request.QueryString["sku"].TrimEnd( '/' );
0
source

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


All Articles