Any downsides to cookie assignment inside custom class using HttpContext.Current? ASP.NET 2.0 cooki

Discussion in 'ASP .Net' started by Guest, Aug 17, 2005.

  1. Guest

    Guest Guest

    Today I learned that creating cookies inside of a custom class in ASP.NET
    2.0 requires that you prefix it with HttpContext.Current..., e.g. :

    HttpContext.Current.Response.Cookies.Add("myNewCookie");

    I am wondering if there are any landmines that I should know about, or if
    this will work pretty much as I am expecting a cookie should.
    Guest, Aug 17, 2005
    #1
    1. Advertising

  2. Guest

    Karl Seguin Guest

    This is true in 1.x as well. Request/Response/... are available in
    codebehind 'cuz your class inherits from Control (via Page/UserControl)
    which exposes this properties. Your custom class, which doesn't inherit
    from such as base class doesn't have these things set up (and it shouldn't).
    So you hook into the page framework via HttpContext.Current.

    Anyways, to get to your question, are there any downside? Well, you are
    tying your custom class to the web, but I can't say if that's a downside or
    not in this situation. Do you ever plan on reusing this class inside a dos
    console, web service or win form?HttpContext.Current will be null in those
    situations, so HttpContext.Current.Response will throw a null reference
    exception. There's nothing wrong with building a this type of dependency,
    but the class shouldn't be considered part of your business layer
    anymore...it is now part of your presentation layer.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    <> wrote in message
    news:...
    > Today I learned that creating cookies inside of a custom class in ASP.NET
    > 2.0 requires that you prefix it with HttpContext.Current..., e.g. :
    >
    > HttpContext.Current.Response.Cookies.Add("myNewCookie");
    >
    > I am wondering if there are any landmines that I should know about, or if
    > this will work pretty much as I am expecting a cookie should.
    >
    >
    >
    Karl Seguin, Aug 17, 2005
    #2
    1. Advertising

  3. Thanks for Karl's informative inputs.

    Hi kenfine,

    I think Karl's suggestions are quite reasonable. The behavior is just the
    same no matter in asp.net 1.x or 2.x. Also, it's better that we guarantee
    the component is used in asp.net web application only since the HttpContext
    won't exists in other desktop or service app.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | From: "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net>
    | References: <>
    | Subject: Re: Any downsides to cookie assignment inside custom class using
    HttpContext.Current? ASP.NET 2.0 cookie fix?
    | Date: Tue, 16 Aug 2005 22:53:15 -0400
    | Lines: 38
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
    | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
    | X-RFC2646: Format=Flowed; Response
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 206-248-153-84.dsl.teksavvy.com 206.248.153.84
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:118483
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | This is true in 1.x as well. Request/Response/... are available in
    | codebehind 'cuz your class inherits from Control (via Page/UserControl)
    | which exposes this properties. Your custom class, which doesn't inherit
    | from such as base class doesn't have these things set up (and it
    shouldn't).
    | So you hook into the page framework via HttpContext.Current.
    |
    | Anyways, to get to your question, are there any downside? Well, you are
    | tying your custom class to the web, but I can't say if that's a downside
    or
    | not in this situation. Do you ever plan on reusing this class inside a
    dos
    | console, web service or win form?HttpContext.Current will be null in
    those
    | situations, so HttpContext.Current.Response will throw a null reference
    | exception. There's nothing wrong with building a this type of
    dependency,
    | but the class shouldn't be considered part of your business layer
    | anymore...it is now part of your presentation layer.
    |
    | Karl
    |
    | --
    | MY ASP.Net tutorials
    | http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    | http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    | come!)
    |
    |
    | <> wrote in message
    | news:...
    | > Today I learned that creating cookies inside of a custom class in
    ASP.NET
    | > 2.0 requires that you prefix it with HttpContext.Current..., e.g. :
    | >
    | > HttpContext.Current.Response.Cookies.Add("myNewCookie");
    | >
    | > I am wondering if there are any landmines that I should know about, or
    if
    | > this will work pretty much as I am expecting a cookie should.
    | >
    | >
    | >
    |
    |
    |
    Steven Cheng[MSFT], Aug 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nayt Grochowski
    Replies:
    1
    Views:
    428
    Nayt Grochowski
    Aug 1, 2003
  2. Dee
    Replies:
    2
    Views:
    376
  3. Rodusa
    Replies:
    0
    Views:
    5,248
    Rodusa
    Aug 30, 2005
  4. Replies:
    4
    Views:
    1,002
  5. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    296
    Teemu Keiski
    Mar 3, 2004
Loading...

Share This Page