This post originated from an RSS feed registered with .NET Buzz
by Scott Hanselman.
Original Post: Am I running in DesignMode?
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
Well, crap. It's 10:10pm. I'm at work. I missed the Portland
[.NET] Bloggers Dinner. It was one of those coding things...just one more
class...then I'll get validation working...then I'll add configurability...you know.
Poop.
But, I did learn something interesting that wasn't obvious to yours truly. I
have a class heirarchy for my ASP.NET pages so the developers that use my infrastructure
to build bankings sites have a few things always available to them. The pages
derive from blah.blah.blah.SharedBasePage (ya, I know it's a lame name) which derives
from LocalizedPage. It happens to be a heritage that spans 3 related assemblies.
They all used to be in the same Web Project, but I started pushing them up in my recent
mad refactoring.
Now, at SOME point along the line, I lost Forms Design Time support in VS.NET.
I started getting this doozy: "The designer could not be shown for this
file because none of the classes within it can be designed."
This perplexed me, but really, I don't care since I'm all about HTML and asp:this
and asp:that. But, my users/developers will care, so it was eating at me.
The problem was, I couldn't remember when I broke it.
Yada yada yada, it turns out I had a line like this in a constructor up the chain:
And when VS.NET switches into Design Mode, it actually INSTANTIATES the page class. (Read
that again if you didn't get it, I didn't.)
So, of course at Design Time, the HttpContext.Current is null. So, that line
now looks like this:
>if (HttpContext.Current
!= null) resmgr
= ((System.Resources.ResourceManager)HttpContext.Current.Application[Constants.RESOURCESTRINGS]);
else
System.Diagnostics.Trace.WriteLine("Note: We are NOT running with valid HttpContext.
We are probably in the IDE.
Or, something is wrong with the flux capacitor.");>
>
So, magically, all my Design Time support is back, and I'm actually using it.
Maybe I wasn't such an HTML bad-ass after all.