Click here to Skip to main content
12,394,573 members (72,904 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
//this code in new class called utility , not in form load , when i need to write request or response in utility class it never written just "response or request " , i see only httpresponse i need to see just response , I hope that you explain to me in detail what exactly do so there are not mistakes, and execute in utilitys class >>>>>>>> look in Comment below.


public class utilitys
{

public void createCookie(string cookieName, string[] keys, string[] values, bool expired)
{

HttpCookie c = new HttpCookie(cookieName);
if (keys != null)
{
for (int x = 0; x < keys.Length; x++)
c.Values.Add(keys[x], values[x]);
if (!expired)
c.Expires = DateTime.Now.AddYears(5);
}
else
c.Expires = DateTime.Now.AddYears(-5);
// look in this step >>>>>>>
res.Cookies.Add(c);


}

public string readFromCookies(string cookieName, string key, HttpRequest req)
{
try
{
return req.Cookies[cookieName][key].ToString();
}
catch
{
return null;
}
}

public void removeCookie(string cookieName, HttpResponse res)
{
// and look in this step >>>>>>>>>>>>>>>
createCookie(cookieName, null, null, false, res);

}



}</pre>
Posted 12-Jul-13 18:04pm
Updated 12-Jul-13 19:24pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you can replace res with HttpContext.Current.Response in createCookie method
HttpContext.Current.Response.Cookies.Add(c);
OR
you can add new HttpResponse parameter to the method and pass it to the method as parameter
 public void createCookie(string cookieName, string[] keys, string[] values, bool expired, HttpResponse res)
{
    HttpCookie c = new HttpCookie(cookieName);
    if (keys != null)
    {
        for (int x = 0; x < keys.Length; x++)
            c.Values.Add(keys[x], values[x]);
        if (!expired)
            c.Expires = DateTime.Now.AddYears(5);
    }
    else
        c.Expires = DateTime.Now.AddYears(-5);
    // look in this step >>>>>>>
    res.Cookies.Add(c);
}
  Permalink  
v5
Comments
dongos931 13-Jul-13 2:10am
   
realy i you help me thank you very muck
Mahesh Bailwal 13-Jul-13 3:36am
   
Your Welcome. Please mark this question solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100