当我使用reponse.redirect时,如何处理#字符 [英] how Can I handle the # character when I use the reponse.redirect

查看:106
本文介绍了当我使用reponse.redirect时,如何处理#字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用response.redirect(url)并且url具有#字符时,我遇到了问题...网络服务器正在取出#字符后面的任何字符#



我知道如何处理它

解决方案

我无法使用response.redirect复制问题( urlstring):



  Dim  gotourl 作为 Uri =  Uri(  http://www.red.com#hashthatstays
Response.Redirect(gotourl.ToString)





...到达页面http://www.red.com#hashthatstays



但是,如果您要链接的文件有文件名中的哈希值:



http://www.google.com/bob#ofcars.htm,然后您需要将哈希字符转换为URL实体%23。这是因为服务器将搜索文件或文件夹bob,一旦bob打开,浏览器将尝试转到ofcars.htm书签。



http://www.google.com/bob%23ofcars.htm会在此示例中打开正确的文件(如果google在根文件夹中有一个名为bob#ofcars.htm的文件。它没有,我查了一下; o)



这里有一个完整的URL实体列表: http://www.w3schools.com/tags/ref_urlencode.asp [ ^ ]



如果您的文件名包含散列和带书签的网址,这会变得棘手:



网址http://www.google.com/bob#ofcars.htm#fords需要转换为



http://www.google.com/bob%23ofcars.htm#fords



在浏览器中正确加载 - 例如打开文档并滚动到书签fords。转义第二个哈希会导致它成为服务器尝试查找的文件名的一部分,这将导致链接断开。



正则表达式和替换可以帮助在保留URL书签的同时解析带有哈希值的文件名。


你需要编码/解码网址。



String url =http ://www.red.com#hashthatstays;

string encodedURL = HttpUtility.UrlEncode(url);

Response.Redirect(encodedURL);


尝试 UrlEncode



Response.Redirect(HttpUtility。以UrlEncode(URL));

I am having a problem when I try to use the response.redirect(url) and the url has the # character ... the webserver is taking out the # character plu any character that is after the #

Any idea how i can handle it

解决方案

I wasn't able to duplicate the problem using response.redirect(urlstring):

Dim gotourl As Uri = New Uri("http://www.red.com#hashthatstays")
Response.Redirect(gotourl.ToString)



...arrives at the page http://www.red.com#hashthatstays

However, if the file you're linking to has a hash in the filename:

http://www.google.com/bob#ofcars.htm, then you'll need to convert the hash character into the URL entity %23. This is because the server will be searching for the file or folder "bob", and the browser will attempt to go to the bookmark "ofcars.htm" once "bob" is opened.

http://www.google.com/bob%23ofcars.htm would open the correct file in this example (if google had a file named bob#ofcars.htm in the root folder. It doesn't, I checked ;o)

There's a full list of URL entities here: http://www.w3schools.com/tags/ref_urlencode.asp[^]

This gets tricky when you have a filename that contains a hash and a url with a bookmark:

The URL http://www.google.com/bob#ofcars.htm#fords needs to be transformed into

http://www.google.com/bob%23ofcars.htm#fords

to load properly in the browser - e.g. open the document and scroll to the bookmark "fords". Escaping the second hash causes it to become part of the filename the server attempts to find, and that'll result in a broken link.

Regular expressions and replacements can help parse a filename with hashes while preserving URL bookmarks.


You need to encode/decode url.

String url = "http://www.red.com#hashthatstays";
string encodedURL= HttpUtility.UrlEncode(url);
Response.Redirect(encodedURL);


try with UrlEncode

Response.Redirect(HttpUtility.UrlEncode(url));


这篇关于当我使用reponse.redirect时,如何处理#字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆