Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//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
Updated 12-Jul-13 19:24pm
v2

1 solution

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);
}
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900