简体   繁体   中英

ASP C#: Special characters: #, $, + cant pass trough URL parameter

I need to get file from virtual folders. I have GetDocumentPreview.aspx which can do it. The parameters are URL type. Below good one:

/GetDocumentPreview.aspx?name=filename&type=jpg

//But if I ask for file with #, &, + in name:

/GetDocumentPreview.aspx?name=&filename&type=jpg

/GetDocumentPreview.aspx?name=#filename&type=jpg

/GetDocumentPreview.aspx?name=+filename&type=jpg

I have no file, because of characters #, &, + (I've tested all characters).

How can I pass #, &, + to URL parameter. I need to use URL parameter because i call this class from Javascript hover tooltip with images.

You need to escape those special chars :

This function does it best :

在此处输入图片说明

Be careful , don't use UrlEncode

在此处输入图片说明

You should encode special characters if you're placing them into a URL.

You can use HttpServerUtility.UrlEncode on your string URL before placing it into the hyperlinks/buttons redirect location. An example would be:

string destinationURL = "http://www.contoso.com/default.aspx?user=specialCharacters";

NextPage.NavigateUrl = "~/Finish?url=" + Server.UrlEncode(destinationURL);

In your case, you should UrlEncode the filenames before placing them into the URL string, the special characters mentioned will be used as:

& = %26
+ = %2B

For more you can see here: http://msdn.microsoft.com/en-us/library/zttxte6w(v=vs.110).aspx

Use urlencoded value of these characters. A list can be found here -

http://www.w3schools.com/tags/ref_urlencode.asp

for example, & would be - %26 .

So, /GetDocumentPreview.aspx?name=&filename&type=jpg would be - /GetDocumentPreview.aspx?name=%26filename&type=jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM