Why don't my Html Helpers have intellisense?
Asked Answered
E

11

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?

Elephus answered 24/2, 2011 at 20:23 Comment(0)
M
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.

Mainis answered 24/2, 2011 at 20:34 Comment(5)
You are SO right. Restarted Visual Studio and it works right away.Elephus
Many thanks to you! I've at least spent an hour on that one today :)Elephus
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 :)Moshe
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.Ftc
I'm using Visual Studio 2013 Update 1 and it also happens. Restarting VS2013 make it works again.Beabeach
U
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>
Unspoiled answered 24/2, 2011 at 20:27 Comment(1)
unload and reload the project.Ftc
C
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!

Cioban answered 1/9, 2011 at 15:50 Comment(1)
This worked, thanks! Much better than restarting VS, because we all know that takes forever! :)Disgusting
M
1

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" />
Miso answered 15/9, 2021 at 10:7 Comment(0)
A
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.

Astomatous answered 24/2, 2011 at 20:25 Comment(0)
H
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.

Hollandia answered 24/2, 2011 at 22:45 Comment(0)
Z
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?

Zendejas answered 18/1, 2012 at 7:54 Comment(0)
O
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...

Oney answered 14/3, 2012 at 21:13 Comment(0)
A
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 :)

Adiaphorism answered 6/3, 2014 at 9:38 Comment(0)
M
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.

Marchpane answered 21/9, 2016 at 23:41 Comment(0)
P
0

In addition to the changes suggested by Josip Stavalj, I needed to make a change to the config section of Views/Web.config. Specifically I needed to change these lines:

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

and change the version string from "2.0.0.0" to "3.0.0.0". I'd been struggling with this for days, thanks for setting me on the right path, Josip.

Paschall answered 24/7, 2024 at 18:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.