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

G

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.
 
K

Karl Seguin

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
 
S

Steven Cheng[MSFT]

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: <[email protected]>
| 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: <[email protected]>
| 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!)
|
|
| | > 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.
| >
| >
| >
|
|
|
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top