views:

173

answers:

2

I am trying to debug a problem where HtmlHelper is null in extension methods. Oddly the debugger claims it's fully populated, however I consistently get null exceptions. I'm trying to figure out where HtmlHelper should being instantiated in an attempt to see where my problem may be.

Where should HtmlHelper be Instantiated?

Update: In particular I am trying to implement the extension found here, http://chriscavanagh.wordpress.com/2009/06/11/mvc-authorizedactionlink/, within a masterpage. The error occurs on the MVC template's HomeController.Index(). There are some 'plugins'/virtualpathing that may be causing the problem (trying to avoid this can of worms), but, code the code is essentially the same as found here: http://www.codeplex.com/unifico. However, I don't want to trouble anyone with the details of all of that.

+3  A: 

The HtmlHelper is instantiated internally by ASP.NET MVC - it's not something you should generally need to worry about. Exactly where it gets instantiated depends on where you are using it.

The main place it gets instantiated is ViewPage's InitHelpers() method.

In ViewUserControls it gets created on-demand in the getter of the Html property.

In ViewMasterPages it just uses the ViewPage's Html property.

Eilon
Ahh, thanks. Seems to indicate my viewcontext is null...
ccook
It's very difficult for ViewContext to be null - there are a number of safety checks along the way in the code to ensure that it doesn't happen. I recommend simplifying the code until you find out the exact cause. My guess is there's some code that's doing some pretty crazy stuff here.
Eilon
Yea, I'm going to do just that... What really gets me is that the debugger and the (runtime?) disagree. The debugger will show proper values in the extensions. Thanks again
ccook
It looks like its some kind of interaction with virtual pathing. I'm going a different route (heh). Thanks for the info
ccook
+1  A: 

We've encountered this too (oddly, only on 1 of 3 machines), where we have a master page and the content page uses an HtmlHelper.DropDownList. What worked for us was adding using System.Web.Mvc to the master page's codebehind even though it was already in the content page's codebehind.

Ryan
I never really did track it down... I recreated the same project and the problem didnt present itself...
ccook