【发布时间】:2014-05-30 11:23:09
【问题描述】:
我有一个应用程序 URL,可以通过传入的查询字符串从浏览器启动。
我的开发网址是
http://localhost:15094/MyPage.html?user=username&role=admin
客户端 URL 将是硬盘中 MyPage.html 的路径
file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin
当 url 是带有 http 的 localhost 时,我可以使用
提取查询字符串System.Windows.Browser.HtmlPage.Document.DocumentUri.Query
// this gives me ?user=username&role=admin
// from http://localhost:15094/MyPage.html?user=username&role=admin
但是当客户端使用 URL 时我想要 ?user=username&role=admin
file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin
我想 System.Windows.Browser.HtmlPage.Document.DocumentUri.Query 不适用于它。
请注意,开发 URL 带有 http,当应用程序安装在客户端的机器上时,该 url 将不带 http,这就是应用程序的工作方式。请不要建议将其托管在 IIS 等中。
我的问题很简单:
如何从“file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin”中提取查询字符串? 如果 System.Windows.Browser.HtmlPage.Document.DocumentUri.Query 对于没有 http 的 url 工作正常,为什么我没有得到正确的查询字符串?
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
你看过
System.Uri类吗? -
如果我使用 System.Uri,它将 MyPage.htmn?user=username 替换为 MyPage.html%3Fuser=username :( 所以没有运气。基本上“?”将转换为“% 3F" 当 URL 没有 http
-
@John
System.Windows.Browser.HtmlPage.Document.DocumentUri.Query是System.Uri。 @Vinnie 最有可能完成百分比编码,因为 Windows 上的路径可能不包含问号。另见file URI scheme on Wikipedia。我认为这里真正的问题是你是否真的想要这样的 URI,但我确定你是否必须使用原始 URI 字符串上的一些IndexOf()和Substring()来获取查询部分。 -
@CodeCaster - 这是我可以利用的可能性之一。但是当您有一个非常大的查询字符串并传入大量参数时,HttpUtility.ParseQueryString(uri.Query) 将非常有助于通过指定键来获取参数值。 Uri uri = new Uri(url, UriKind.Absolute); var newQueryString = HttpUtility.ParseQueryString(uri.Query); sQSValue = newQueryString[key].ToString();
标签: c# url query-string