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

纵然是瞬间 提交于 2019-12-05 20:47:27

Actually, your should encode your values for URLs with HttpServerUtility.UrlEncode method:

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

URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. As a result, these characters must be encoded in tags or in query strings where the strings can be re-sent by a browser in a request string.

EDIT:

let's check this with the values you provided: name = Bellagio™ 16 1/2" High Downbridge Outdoor Wall Light, sku = 46910: firstly I created a page with 2 properties:

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

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

and then add link definition to the page:

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

and then grab these values (click the link firstly):

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

these values are exactly the same as you provided: Bellagio™ 16 1/2\" High Downbridge Outdoor Wall Light and 46910.

Unfortunatelly, I was unable to reproduce an incorrect URL you post in your first comment: LifeSizePDF.aspx?productname=Bellagio&amp;%238482%3b+16+1%2f2&amp;quot%3­b+High+Downbridge+Outdoor+Wall+Light&amp;shortsku=46910%2f

Use URL encoding to format the values appropriately, assuming the forward slash in the name is intentional and would like to be extracted with the end result.

Note that there are at least two ways to go about this easily, for example using the static class HttpUtility or, when in the context of a Page, using the Server property:

var encodedValue = HttpUtility.UrlEncode(rawValue);

var encodedValue = Server.UrlEncode(rawValue);

You could just trim the end value of the sku request:

Request.QueryString["sku"].TrimEnd( '/' );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!