2012-03-09 67 views
0

有沒有辦法在.NET中使用這些字符?.NET轉義字符?

我發送一個字符串包含PHP代碼通過ajax到.NET方法。

然後,此方法通過電子郵件發送,而是因爲他們在jQuery的已經逃脫,它看起來像:

<?php print $test; ?> 

Looks like: 

%3C%3Fphp%0A%0Aprint%20%24test%3B%0A%0A%3F%3E 

所以基本上我需要取消轉義它是通過電子郵件發送之前。


編輯

仍然無法得到這個工作,仍然不會發送字段的內容。

public static string CreateSnippet(int processid) 
{ 

    //here we find form values posted to the current page 
    HttpRequest post = HttpContext.Current.Request; 
    string email = post["email"]; 
    string comment = post["comment"]; 
    string name = post["name"]; 
    string website = post["website"]; 

    var DecodedString = HttpContext.Current.Server.UrlDecode(comment); 

    var mailcontent = new StringBuilder(); 
    mailcontent.AppendFormat(EmailLineFormat, "Name", name); 
    mailcontent.AppendFormat(EmailLineFormat, "Email", email); 
    mailcontent.AppendFormat(EmailLineFormat, "Website", website); 
    mailcontent.AppendFormat(EmailLineFormat, "Message", DecodedString); 

    // Send the email 
    library.SendMail("[email protected]", "[email protected]", "Contact Form Enquiry", mailcontent.ToString(), true); 

    //if nothing gets created, we return zero 
    return "success"; 

} 

我是以正確的方式使用它還是存在與我設置變量的方式有關的問題?

感謝 羅伯特

+0

這個稱謂使我的突破爆炸。 – Hamish 2012-03-09 00:38:26

回答

2

在JavaScript中的字符串或URL解碼的.NET中的等價物(Server.UrlDecode(EncodedString)

+0

感謝您的回覆。但如果我在javascript中使用decodeURIComponent它不會通過Ajax發送到.net方法? 這是在ajax請求之前將其轉義的原因。 我需要在.NET中完成它。只需要找出實現方法。 – Robert 2012-03-09 00:30:33

+0

嘗試'Server.UrlDecode(EncodedString)'作爲友好的編輯指出。我個人不熟悉.NET。 – 2012-03-09 00:33:02

+0

@Robert我編輯了答案的.NET解碼方法(這包含在System.Web中)。 – 2012-03-09 00:33:09

0

URLDecode是做了正確的方式,但只是爲了好玩用decodeURIComponent這裏是一個正則表達式單線程:

Regex.Replace(input, "%(..)", m => ((char)Convert.ToInt32(m.Groups[1].ToString(), 16)).ToString());