如何对时间段进行URL编码?

问题描述 投票:23回答:2

我需要对一些句点进行URL编码,因为我必须传递一些文档路径,就像这样

http://example.com/test.aspx?document=test.docx

因此test.docx导致我出现非法字符错误。所以我需要改变它

.  -->  %2E

我试图使用Server.UrlEncode

  string b = Server.UrlEncode("http://example.com/test.aspx?document=test.docx");

但我明白了

"http%3a%2f%2fexample.com%2ftest.aspx%3fdocument%3dtest.docx"

那么我是否必须像字符串替换一样使用并手动执行并用该代码替换所有句点?

c# urlencode url-encoding
2个回答
17
投票

那段时间没有他的问题(假设%2E没有解决问题)。句点是一个完全有效的URL字符,无论问题是什么,它都不是句号。检查正在抛出的错误的堆栈跟踪或发布完整的错误详细信息。

而且您不应该对整个路径进行URL编码。只有查询字符串参数值。

string b = "http://example.com/test.aspx?document=" + Server.UrlEncode("test.docx");

如果你这样试试,你仍然会收到错误吗?

我不会用十英尺的杆子碰到SharePoint。但是,逃避这段时间并不一定会阻止SharePoint做它的诡计。但我想你至少应该尝试一下。

Server.UrlEncode("test.docx").Replace(".", "%2E");

25
投票

这是一个非常古老的问题,但我遇到了类似的问题。我在我的网址的末尾加了一个“/”,其中包含了句号,它解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.