PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

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

 
 
Guest
Posts: n/a
 
      17th Aug 2005
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.



 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      17th Aug 2005
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!)


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      17th Aug 2005
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: <(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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!)
|
|
| <(E-Mail Removed)> wrote in message
| news:(E-Mail Removed)...
| > 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.
| >
| >
| >
|
|
|

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cookie contanier does not correctly parse Set-Cookie header with domain token dav Microsoft Dot NET Framework 2 15th Dec 2010 08:55 AM
cookie contanier does not correctly parse set cookie header with domain token dav Microsoft Dot NET Framework 0 12th Aug 2007 09:06 AM
Persistent cookie overwrite session cookie =?Utf-8?B?TWFyaW8=?= Windows XP Internet Explorer 1 22nd Sep 2006 05:50 PM
Cookie Question (IP as domain and cookie file location) =?Utf-8?B?UGF1bA==?= Microsoft ASP .NET 1 10th Jan 2006 08:37 PM
Transient-Cookie/Session-Cookie and multiple browser instances X Windows XP Internet Explorer 7 29th Apr 2004 11:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:24 PM.