168

I don't know if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper, it is not recognized in a Razor powered page:

namespace SomeNamespace.Extensions {
    public static class HtmlExtensions {
        public static string Foo(this HtmlHelper html) {
            return "Foo";
        }
    }
}

I added it to the <Namespaces> section in Web.config:

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <!-- snip -->
    <add namespace="SomeNamespace.Extensions"/>
  </namespaces>
</pages>

But it throws a compile error when trying to view the page:

@Html.Foo()

If I recreate the page with WebForms it works fine. What's the deal?

Workaround

If I include @using SomeNamespace.Extensions in my Razor view, then it works, but I'd much rather just have it in Web.config

peterh
  • 11,875
  • 18
  • 85
  • 108
swilliams
  • 48,060
  • 27
  • 100
  • 130
  • 2
    Even the workaround doesn't work for me. The extension method will show up in intelliSense, but throws a compile error during runtime. – Clark Nov 15 '10 at 16:44
  • +1 for the @using workaround. I prefer it in my case. – Kent Dec 11 '12 at 23:17
  • @using workaround is ok for a single view. But for multiple views the code management becomes an issue. – Rahatur Dec 05 '13 at 06:53

8 Answers8

274

Since the Beta, Razor uses a different config section for globally defining namespace imports. In your Views\Web.config file you should add the following:

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

<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" />
      <!-- Your namespace here -->
    </namespaces>
  </pages>
</system.web.webPages.razor>

Use the MVC 3 upgrade tool to automatically ensure you have the right config values.

Note that you might need to close and reopen the file for the changes to be picked up by the editor.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
marcind
  • 52,944
  • 13
  • 125
  • 111
  • Bingo. Thank you! Do you know if that is how it's always going to work or will it be changed in RTM? – swilliams Nov 09 '10 at 18:53
  • @swilliams At this point there's a really low probablility that we would change this. It will almost certainly work the same way in RTM. – marcind Nov 09 '10 at 19:31
  • And how would this work with Areas, if I want to share a namespace with all my Areas? With the old engine I can add my namespaces in the root web.config, which takes care of that, but how would this work with Razor? – Gideon Nov 11 '10 at 16:35
  • 1
    Am I missing an assembly reference? It doesn't recognize System.Web.Mvc.WebViewPage or any of the types in the configSections section. – Clark Nov 15 '10 at 16:48
  • @Clark the type name is correct. Are you sure you are using MVC 3? – marcind Nov 15 '10 at 19:43
  • 1
    Yes I am using MVC 3. I think resharper was falsely highlighting System.Web.Mvc.WebViewPage and all of the types in the configSections section. That may be a symptom of the actual problem. If I comment out the line in the view that is using the extension method it runs fine, but if I try to use the extension method I get a compilation error. – Clark Nov 15 '10 at 22:30
  • 4
    I had to add a reference to >System.Web.WebPages.Razor and it sorted it out. – TWith2Sugars Nov 18 '10 at 12:27
  • 4
    It's not working for me. I'm using MVC 3 RC , I added my namespace to the web.config in my views folder. Still can't see my extensions in Razor page. Event putting @using doesn't work. @ TWith2Sugars I can't even find System.Web.WebPages.Razor to add , I added System.Web.Razor but did not help. I'm really stuck here – freddoo Dec 04 '10 at 23:58
  • 32
    +1 for telling me to open and close the file to see the updates. – Amir Jan 20 '11 at 00:19
  • @marcind "Magic reopen" applied and for RTM too. Hopefully it will be fixed sometime, is it Visual studio issue?. – angularrocks.com Feb 27 '11 at 07:39
  • If I include the `` statement above and/or both code blocks, I get "cannot start debugging on the web server, please check your web.config" etc. I take it out, works fine. I am using MVC 3. Any thoughts? – GONeale Apr 26 '11 at 10:24
  • 26
    Thank you for pointing out this is not the root web.config, but the web.config in the View folder! – G-Wiz Apr 29 '11 at 04:46
  • Just wanted to let everyone know you can't include a direct file, has to be the namespace >. – Nate-Wilkins Apr 26 '13 at 21:23
11

As the accepted answer suggests you can add "using" to all views by adding to section of config file.

But for a single view you could just use

@using SomeNamespace.Extensions

Paul Rowland
  • 8,244
  • 12
  • 55
  • 76
6

I had this same error in an MVC 4 application using Razor. In an attempt to clean up the web.config files, I removed the two webpages: configuration values:

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

Once I restored these configuration values, the pages would compile correctly and the errors regarding the .Partial() extension method disappeared.

John Rasch
  • 62,489
  • 19
  • 106
  • 139
5

I had this issue in VS 2015. The following solved it for me:

Find "webpages:Version" in the appsettings and update it to version 3.0.0.0. My web.config had

<add key="webpages:Version" value="2.0.0.0" />

and I updated it to

<add key="webpages:Version" value="3.0.0.0" />
balint
  • 3,391
  • 7
  • 41
  • 50
Damian Green
  • 6,895
  • 2
  • 31
  • 43
3

I found that putting this section in my web.config for each view folder solved it.

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
Joseph Morgan
  • 163
  • 1
  • 9
  • 1
    I actually found that for some reason the ROOT web.config had a binding redirect which was causing my problem (specifically, pointing `0.0.0.0-2.0.0.0` to `1.0.0.0`), and the `/View/web.config` file does not override that. (Also, I don't believe you need it in every `/View/Subfolder/`) – JoeBrockhaus Sep 04 '13 at 20:48
1

This error tells you that you do not have the razor engine properly associated with your project.

Solution: In the Solution Explorer window right click on your web project and select "Manage Nuget Packages..." then install "Microsoft ASP.NET Razor". This will make sure that the properly package is installed and it will add the necessary entries into your web.config file.

user3459730
  • 369
  • 5
  • 6
0

In my case use VS 2013, and It's not support MVC 3 natively (even of you change ./Views/web.config): https://stackoverflow.com/a/28155567/1536197

Community
  • 1
  • 1
Hernaldo Gonzalez
  • 1,977
  • 1
  • 21
  • 32
-6

Since ASP.NET MVC 3 RTM is out there is no need for config section for Razor. And these sections can be safely removed.

nick4eva
  • 504
  • 4
  • 6
  • 3
    MichaelvR, yes it was my mistake. I'm sorry that you entered in confusion. – nick4eva Jan 18 '11 at 10:54
  • Does this also count for a VB powered MVC project? I have no access to my extension methods... http://stackoverflow.com/questions/4789273/net-mvc3-razor-vb-extensions-not-imported-in-view – Ropstah Jan 25 '11 at 02:55
  • This answer is wrong. These config entries are still required. The new project templates already have them but if you are upgrading an Mvc 2 application you will have to add them. – marcind Feb 27 '11 at 17:22
  • I wonder if that is true, nick4eva. If I create a new MVC 3 web application, and chose the Razor viewengine, the web.config, which is present in the Views folder, will have the razor configSections by default. If I disable them, I will get compile errors at runtime. – MichaelvR Jan 16 '11 at 14:24