问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/1996186/location-of-htmlhelper-instantiation-for-asp-net-mvc