System.Web.WebPages Aren’t the HtmlHelpers You’re Looking For…

Had a bit of a frustrating morning, one of those times when your tools work against you by helping you out but cost you 35 minutes and an extra cup of coffee.  And maybe a sanity check or two.  The goal was to create a very simple HtmlHelper extension method.  I kept getting the error:

‘System.Web.Mvc.HtmlHelper<dynamic>’ does not contain a definition for ‘MethodName’ and the best extension method overload ‘Namespace.MethodName(System.Web.WebPages.Html.HtmlHelper)’ has some invalid arguments

TL;DR – Check your using statements.  If you’re sharp, the error’s quite indicative of what’s going on.

Here’s another hint as to why this went south on me:

image

I was a little quick on the CTRL + . this morning, and since the first item in the list was System.Web.WebPages.Html, the tools didn’t help me out much at all.  But hey…shouldn’t ReSharper be able to figure this one out for me and give me the light bulb of awesome? 

image

I digress…

Regardless of trying to put using statements in my views for my namespace, or adding the namespace to my web.config in the views folder, the problem was I had this guy:

using System.Web.WebPages.Html;

…but I really wanted this guy:

using System.Web.Mvc;

Fixing that using in the top of my helper class resolved the issue, as now I was actually targeting the proper HtmlHelper class, in the correct namespace, that is being used by my views.  Hope this helps someone else out there as well.