Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 702871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:46:07+00:00 2026-05-14T03:46:07+00:00

hi i defined one class to create cookie by received parameter’s from user. when

  • 0

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.


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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:46:07+00:00Added an answer on May 14, 2026 at 3:46 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I defined a class that I want to use for building a window. One
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I am having trouble getting a variable from one defined function to another. For
I'm having trouble passing a variable defined in one class instance to another class
I want to expose a .NET class to COM. That's fairly easy: I create
I have a scenario where I need to bind two properties from one class
I have a function like this defined in one class: using System; using System.Collections.Generic;
I defined two classes. First one... [Serializable] public class LocalizationEntry { public LocalizationEntry() {
If I have the core of a class defined in one file as public
I want to create a class, ClassB , as inner class of ClassA ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.