Home Dashboard Directory Help

ASP.NET Abstractions: add HttpContextBase.Current by AtsushiEno


 as By Design Help for as By Design

Sign in
to vote
Type: Suggestion
ID: 376426
Opened: 10/19/2008 3:25:12 AM
Access Restriction: Public


There should be some replacement for HttpContext.Current in ASP.NET abstractions layer.
Sign in to post a comment.
Posted by Adrian Anttila on 5/1/2009 at 2:07 PM
As an alternative, you can use the following line of code to create an HttpContextBase class:

HttpContextBase context = new HttpContextWrapper(HttpContext.Current);

Hope this helps,
Posted by Microsoft on 2/19/2009 at 3:49 PM
Hi there! Thanks for the suggestion. This is by design.

Suppose HttpContextBase.Current existed.

To logically mimic HttpContext.Current, the property would have to be static. If the property is static you couldn’t mock it anyway! Even if it’s settable that is unacceptable to unit tests since you never want to use statics (they are global and affect global state). Unit tests should not affect global state in such a way as that could affect other unit tests. Unit tests need to be isolated and independent.

If something in Dynamic Data isn’t testable, it has nothing to do with System.Web.Abstractions, but probably something specific to Dynamic Data itself. I didn't actually see a specific problem in this bug report, but it might be worth reporting to the Dynamic Data team if you have something specific. For example, in MVC we always pass our contexts along and never use HttpContext.Current. If Dynamic Data did the same then that might solve the problem in their case.
Sign in to post a workaround.