Asp. NET gets no cookie solution written by JS

Source: Internet
Author: User
Tags httpcontext key net return setcookie string

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.