Use JavaScript tonight to set an antecedents cookie, and then use asp.net to get this cookie value, found that asp.net get no JS settings cookie value, really depressed, the following is JS write cookie code:
Code
Write Cookies
function Setcookie (name,value)
{
document.cookie = name "=" Escape (value) ";"; Set the cookie off browse that is invalid
}
//js settings are routed to the cookie
setcookie ("Fromurl", document.referrer);
The following is the ASP.net read cookie code:
Code
<summary>
///Read cookies
///</summary>
///<param name= "str" ></param>
/ <returns></returns> public
string getcookies (string key)
{
if ( System.web.httpcontext.current.request.cookies[key]!= null)
{
string _cstr = System.web.httpcontext.current.request.cookies[key]. Value.tostring ();
Encoding stre = encoding.getencoding ("UTF-8");
Return System.Web.HttpUtility.UrlDecode (_cstr, stre);
}
else
{return
string. Empty
}
}
Gets
the cookie string referer = GetCookies ("Fromurl");
Here asp.net use UTF-8 encoding to decode the value of the JS write cookie, or the cookie will be a little different.
Is the following code appears asp.net incredibly get not to JS write cookies, then how to do? Don't worry, here's the solution:
Re-change the JS code, as follows:
Code [
Write Cookies
function Setcookie (name,value)
{
document.cookie = name ' = ' Escape (value) '; path=/"; The cookie set is off browsing is invalid
}
Add a path=/, on the line. It's as simple as that.
This set the Path property of the cookie, you must set the Path= "/" attribute on both the client and the server, so that the operation of JS and asp.net can interact with cookies! Another is the problem of coding, where the encoding in JS is encodeuri corresponding to the ASP.net server.urlencode.
Author: Dodo
Original: http://www.xueit.com/html/2009-12-06/21-984180469828.html
The copyright of this article and the learning it Network are common, welcome to reprint, but without the consent of the author must retain this paragraph of the statement, and retain the original link. Otherwise, the right to pursue legal liability is retained.