4
votes

I've been spending the better part of two hours trying to figure this out. Here goes:

I just want to create an HTMLHelper extension method. This has been done many times before, and I know I'm coding it correctly. However, Razor only seems to know about it if I place the code file within App_Code (a folder which I thought no longer had meaning in the MVC world!)

How do I know this?

Quite simply...I have two identically-coded HTMLHelper extension methods in two separate identically-named files.

The first file, placed inside ~/Extensions contains the extension method DisplayForProperty

The second file, placed inside ~/App_Code contains the extension method DisplayForProperty2

When in a view, I type @Html.Displ- Intellisense only shows me DisplayForProperty2!

I do not have a @using statement, as that does not appear to be necessary to see DisplayForProperty2. Not to mention, it doesn't change anything in this case.

So, why is this? Should this even be happening this way? Is this just Razor being retarded, or am I the one being retarded?

I'm using a fully-updated copy of VS 2012.

PS - On another note, do I have to place the namespaces reference in every web.config under every /Views directory (as in, in every single Area), plus at the root? Why can't I just put this in the root or in the root /Views directory?

4
Is this a web site "project"? If so, then try this using a web application project.John Saunders
This project was started by using the ASP.NET Internet Application template in Visual Studio 2012.emkayultra
You can put the namespace reference in the Web.config at the root of your application, or (even better) in the Web.config at the root of your ~/Views/ folder alongside the default ones referenced by MVC. The configuration will be inherited by any subdirectories.user22467

4 Answers

2
votes

You shouldn't have to put the html helper extension methods inside of App_Code. While a little old, the tutorial on asp.net: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs guides you pretty well.

I typically have a "Helpers" folder under the root of the MVC project with separate class files for each. They're in the Project.MVC.Helpers namespace and I add a @using Project.MVC.Helpers to the Razor views I want to use the helper in.

0
votes

The app_code folder is still a special folder which is automatically compiled and referenced by the project.

0
votes

Did you check the namespace that was generated on the HtmlHelper class when you put it in a folder outside of App_Code?

Perhaps all that you're missing out is the @using directive to point to that namespace inside your view.

0
votes

You may need to add the namespace that contains your helper to the Web.config file inside your views folder.

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Namespace.Of.Your.Helper" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

This will allow your views to use your helper without a @using that references the namespace.