Razor HtmlHelper Extensions (or other namespaces for views) Not Found

asp.net Mvc-3Extension MethodsRazorHtml Helper

asp.net Mvc-3 Problem Overview


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

asp.net Mvc-3 Solutions


Solution 1 - asp.net Mvc-3

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.

Solution 2 - asp.net Mvc-3

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

Solution 3 - asp.net Mvc-3

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.

Solution 4 - asp.net Mvc-3

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" />

Solution 5 - asp.net Mvc-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>

Solution 6 - asp.net Mvc-3

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.

Solution 7 - asp.net Mvc-3

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

Solution 8 - asp.net Mvc-3

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionswilliamsView Question on Stackoverflow
Solution 1 - asp.net Mvc-3marcindView Answer on Stackoverflow
Solution 2 - asp.net Mvc-3Paul RowlandView Answer on Stackoverflow
Solution 3 - asp.net Mvc-3John RaschView Answer on Stackoverflow
Solution 4 - asp.net Mvc-3Damian GreenView Answer on Stackoverflow
Solution 5 - asp.net Mvc-3Joseph MorganView Answer on Stackoverflow
Solution 6 - asp.net Mvc-3user3459730View Answer on Stackoverflow
Solution 7 - asp.net Mvc-3Hernaldo GonzalezView Answer on Stackoverflow
Solution 8 - asp.net Mvc-3nick4evaView Answer on Stackoverflow