tags:

views:

113

answers:

4

hi i defined one class to create cookie by received parameter's from user. when i want to add cookie to context i receive an exception.

My Class

public static class ManageCookies
{    
    public static void Create(string name, string value)
    {
        HttpCookie cookie = new HttpCookie(name);
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(1);
        HttpContext.Current.Response.Cookies.Add(cookie);
    }

}

Occured Exception: Response is not available in this context.

+2  A: 

The code you provided is not a problem the problem is where you call it from. If you call it after the response is flushed or from a thread other than the one assigned to process your request - you can get all sorts of problems

In response to Sadegh clarification:

This is exactly the point. Application Start happens only once during application lifetime. And I guess you want this cookie to be delivered as a part of every response. In other words on ApplicationStart is not the right place to do it. You can do it at any moment during page lifecycle BEFORE the end of the PreRender

mfeingold
yes, i know that but using this method in ApplicationStart event is an example. i want call PrePareForApplicationStart method to create necessary cookies for first application run! my problem is above method (Create)! thanks dear mfeingold
Sadegh
A: 

hi, actually i want to use this in this class but by another function as below:

public static void PrePareForApplicationStart()
{
        Create("somecookie", "somevalue");
}

also this function called in Application_Start event of Global.asax file.

Sadegh
A: 

hi

i know it is connected with the context within the current sub is executing. i whould suggest my function to pass the current HttpResponse as a parametter to that!

public static class ManageCookies
{    
    public static void Create(string name, string value, HttpResponse response)
    {
        HttpCookie cookie = new HttpCookie(name);
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(1);
        response.Cookies.Add(cookie);
    }
    public static void PrePareForApplicationStart()
    {
        Create("somecookie", "somevalue", _context);
    }
}

this is correct way? why? and are you have another way?

UPDATE: Oppps! using this way still have First Exception! :(( Help Help

ManageCookies manager = new ManageCookies(this.Context);
        manager.PrePareForApplicationStart();

i use above code to send HTTPContext object to defined class. it called from Application_Start event.

and ManageCookies class updated as below:

public class ManageCookies
{
    private HttpContext _context;
    public ManageCookies(HttpContext context)
    {
        this._context = context;
    }
}

i use this variable (_context) to adding cookies!

Sadegh
A: 

hey guys! anybody can't help me?!!! in fact i want write a class which have some method for creating/modifying/deleting cookies! but when i initialize cookie for creating/modifying and want to add that to response i receive an exception! Occured Exception: Response is not available in this context.

Sadegh