Use JavaScript tonight to set up an unsolicited cookie, then use ASP. NET to get the cookie value, found that ASP. js Set cookie value, really depressed, the following is the JS write cookie code:
Code
Write cookies
Functionsetcookie (Name,value)
{
Document.cookie=name "=" Escape (value) ";"; /Set Cookie to close browsing is void
}
JS settings to route to a cookie
Setcookie ("Fromurl", document.referrer);
The following is the ASP. NET Read Cookie code:
Code
<summary>
Read cookies
</summary>
<paramname= "STR" ></param>
<returns></returns>
Publicstringgetcookies (Stringkey)
{
if (system.web.httpcontext.current.request.cookies[key]!=null)
{
String_cstr=system.web.httpcontext.current.request.cookies[key]. Value.tostring ();
Encodingstre=encoding.getencoding ("UTF-8");
ReturnSystem.Web.HttpUtility.UrlDecode (_cstr,stre);
}
Else
{
Returnstring. Empty;
}
}
Get cookies
Stringreferer=getcookies ("Fromurl");
Here, ASP. NET uses UTF-8 encoding to decode the value of the JS write cookie, otherwise the cookie will be slightly different.
Is the following code appears ASP. JS Write cookie, how to do it? Don't worry, here's the workaround:
Re-change the JS code, as follows:
code[
Write cookies
Functionsetcookie (Name,value)
{
Document.cookie=name "=" Escape (value) ";p ath=/";//Cookies set to disable browsing
}
Add a path=/, it's all right. It's so simple.
The path property of the cookie is set, and the Path= "/" attribute must be set on both the client and the server side to be able to interact with the cookie operation by JS and ASP! Another is the problem of coding, here JS in the code for encodeURI corresponding to the Server.URLEncode of ASP.