22

I can't get intellisense for my own html helpers. My CustomHtmlHelpers.cs looks like this:

using System.Web.Mvc;
using System.Text;
using System.Web;

    namespace laget.Web.Helpers
    {
        public static class CustomHtmlHelpers
        {
            //MY HELPERS
        }
    }

and in my Web.config:

    <pages>
      <namespaces>
        <add namespace="laget.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages"/>
        <add namespace="System.Web.Helpers" />
      </namespaces>
    </pages>

If I put <@using laget.Web.Helpers> in my view, I get the intellisense issue fixed.

Should it not be enough with the code in Web.config?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
MikeB
  • 345
  • 1
  • 2
  • 9

10 Answers10

24

Sometimes it doesn't seem to work right away. Try closing the .cshtml file, and re-opening it. Then if that doesn't work, try restarting Visual Studio. Also make sure you actually compiled your project, intellisense won't work with non-compiled helpers.

SomeRandomDeveloper
  • 489
  • 2
  • 13
  • 33
Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
  • 2
    You are SO right. Restarted Visual Studio and it works right away. – MikeB Feb 24 '11 at 20:38
  • Many thanks to you! I've at least spent an hour on that one today :) – MikeB Feb 24 '11 at 20:40
  • Yea I've spent some time on this too, thankfully the ultimate question "Have you tried to turn it off and on again" always pops in my head when something is broken :) – Lukáš Novotný Feb 24 '11 at 20:46
  • Also make sure it's the correct web.config, only works for me when I add to the namespaces in the web.config in the view folder. Also you can avoid the full restart by unloading and reloading the project. – Iain M Norman Aug 22 '11 at 13:38
  • 1
    I'm using Visual Studio 2013 Update 1 and it also happens. Restarting VS2013 make it works again. – outlookrperson Mar 11 '14 at 13:09
6

I'm pretty sure that you're not editing the correct Web.config file.

You need to add your namespace to the one in your Views directory.

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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="laget.Web.Helpers" />
    </namespaces>
  </pages>
</system.web.webPages.razor>
Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
5

You actually don't need to restart Visual Studio in most cases. All you need to do is close the .cshtml file and reopen it!

Alexandros B
  • 1,881
  • 1
  • 23
  • 36
0

So now I will show you the steps

1.Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)

2.Add the MVC (5.0) nuget package ( right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)

3.Close any and all open .cshtml files

4.Right click project -> Properties -> Build -> change Output path to your project “bin/”

5.Add the following minimal Web.config to the root of your class library project ( the web config file is solely needed for intellisense. Configuration (via Web.config) should be done in the WebApplication hosting your ClassLibrary assembly)

6.Clean and Build the solution.

7.Open cshtml file and try now :)

0

I found that if it still doesn't work, you may need to go to the properties of the custom class and change the build action from "content" to "compile". That resolved it for me.

0

It needs it on the local page. I'm pretty sure this has to do with Namespace resolution. It isn't exactly sure what you are referring to without the local using statement.

msarchet
  • 15,104
  • 2
  • 43
  • 66
0

I ran into this today as well. Sometimes just closing the Razor view's window in Visual Studio and re-opening it will do the trick without having to do a full Visual Studio restart.

Ryan Tofteland
  • 913
  • 6
  • 11
0

I try all of this solutions, one more thing which i didnt find is that in root web.config i must change webpages:Version from 2.0.0.0 to 3.0.0.0. Open and close all .cshtml files and it's worked.

 <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
0

I tried to solve an issue like this one yesterday. I had e pre-compiled dll (project name ie: MyHtmlHelpers) containing helpers and lot of other classes.

I had the assembly referenced in the web project and the all "standard"-helpers showed up in intellisense but, even though I added the namespace to both web.config in the root and in the views-folder nothing worked. When running the project helpers works, but not in intellisense.

I added a new class and wrote a new html helper inside the web project, added the namespace to web.config. And that worked.

After some hours add tried my last card, adding the MyHtmlHelpers-project to the same solution as my webproject. That did the trick. I diden't change anything in the configs just added the project to the same solution and changed the reference to point at the project insted of the compiled dll.

Isen't that strange? A VS-bug?

0

I found that i was adding the reference to the wrong web.config. It's not the main config but the web.config in the views directory...

Mario S
  • 11,715
  • 24
  • 39
  • 47