2014-09-29 92 views
5

我有一個控制檯應用程序。我必須從HTML查詢字符串中刪除所有不需要的轉義字符。這裏是我的查詢字符串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(); 

無會給我最好的結果。

回答

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) 這意味着它必須被編碼爲首先是一個數據字符串。 這是一個嚴格的編碼是URL編碼

+1

更好地解釋爲什麼這個工作:查詢字符串部分包含編碼的URL參數('ReturnUrl'),這意味着它必須被編碼爲在首位的數據串。這是一個更嚴格的編碼,UrlEncoding – 2014-09-29 10:09:42

+1

謝謝。我將你的評論添加到答案中。 – Marco 2014-09-29 10:12:25