Scott Hanselman

Am I running in DesignMode?

October 1, '03 Comments [5] Posted in ASP.NET
Sponsored By

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:

resmgr = ((System.Resources.ResourceManager)HttpContext.Current.Application[Constants.RESOURCESTRINGS]);

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.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.