我有一个控制台应用程序。我必须从HTML查询字符串中删除所有不需要的转义字符。这是我的查询字符串
string query="http://10.1.1.186:8085/PublicEye_common/Jurisdiction/Login.aspx?ReturnUrl=%2fPublicEye_common%2fJurisdiction%2fprint.html%3f%257B%2522__type%2522%253A%2522xPad.Reports.ReportDetail%2522%252C%2522ReportTitle%2522%253A%25221%2522%252C%2522ReportFooter%2522%253A%25221%2522%252C%2522ReportHeader%2522%253A%25221%2522%252C%2522CommonFields%2522%253A%255B%255D%252C%2522Sections%2522%253A%255B%257B%2522SectionTitle%2522%253A%2522Sections%2522%252C%2522ShowTitleSection%2522%253Atrue%252C%2522SubSections%2522%253A%255B%257B%2522SubSectionTitle%2522%253A%2522Sub%2520Section%2522%252C%2522ShowTitleSubSection%2522%253Atrue%252C%2522FormGroups%2522%253A%255B%257B%2522FormGroupTitle%2522%253A%2522Form%2520Groups%2522%252C%2522ShowTitleFormGroup%2522%253Atrue%252C%2522FormFields%2522%253A%255B%257B%2522FormFieldTitle%2522%253A%2522Form%2520Fields%2522%252C%2522FormFieldValue%2522%253A%252212%2522%257D%255D%257D%255D%257D%255D%257D%255D%257D&%7B%22__type%22%3A%22xPad.Reports.ReportDetail%22%2C%22ReportTitle%22%3A%221%22%2C%22ReportFooter%22%3A%221%22%2C%22ReportHeader%22%3A%221%22%2C%22CommonFields%22%3A%5B%5D%2C%22Sections%22%3A%5B%7B%22SectionTitle%22%3A%22Sections%22%2C%22ShowTitleSection%22%3Atrue%2C%22SubSections%22%3A%5B%7B%22SubSectionTitle%22%3A%22Sub%20Section%22%2C%22ShowTitleSubSection%22%3Atrue%2C%22FormGroups%22%3A%5B%7B%22FormGroupTitle%22%3A%22Form%20Groups%22%2C%22ShowTitleFormGroup%22%3Atrue%2C%22FormFields%22%3A%5B%7B%22FormFieldTitle%22%3A%22Form%20Fields%22%2C%22FormFieldValue%22%3A%2212%22%7D%5D%7D%5D%7D%5D%7D%5D%7D";
我尝试了以下内容:
string decode = System.Net.WebUtility.HtmlDecode(query);
string decode2 =System.Net.WebUtility.UrlDecode(query);
string decode3=System.Web.HttpServerUtility.UrlTokenDecode(query).ToString();
没有人会给我最好的结果。
答案 0 :(得分:4)
这对我有用:
string decode = HttpUtility.UrlDecode(Uri.UnescapeDataString(query));
HttpUtility
位于命名空间System.Web
中。您可能需要添加引用。
输出:
http://10.1.1.186:8085/PublicEye_common/Jurisdiction/Login.aspx?ReturnUrl=/PublicEye_common/Jurisdiction/print.html?{"__type":"xPad.Reports.ReportDetail","ReportTitle":"1","ReportFooter":"1","ReportHeader":"1","CommonFields":[],"Sections":[{"SectionTitle":"Sections","ShowTitleSection":true,"SubSections":[{"SubSectionTitle":"Sub Section","ShowTitleSubSection":true,"FormGroups":[{"FormGroupTitle":"Form Groups","ShowTitleFormGroup":true,"FormFields":[{"FormFieldTitle":"Form Fields","FormFieldValue":"12"}]}]}]}]}&{"__type":"xPad.Reports.ReportDetail","ReportTitle":"1","ReportFooter":"1","ReportHeader":"1","CommonFields":[],"Sections":[{"SectionTitle":"Sections","ShowTitleSection":true,"SubSections":[{"SubSectionTitle":"Sub Section","ShowTitleSubSection":true,"FormGroups":[{"FormGroupTitle":"Form Groups","ShowTitleFormGroup":true,"FormFields":[{"FormFieldTitle":"Form Fields","FormFieldValue":"12"}]}]}]}]}
Panagiotis Kanavos在评论中提出了一个很好的观点:
查询字符串部分包含编码的URL参数(ReturnUrl), 这意味着它必须首先编码为数据字符串。 这是UrlEncoding
的更严格的编码