148

Is there some sort of magic I need to use to get the namespaces in the pages/namespaces element in the webconfig?

<pages>
    <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="System.Web.WebPages"/>
        <add namespace="System.Web.Helpers"/>
        <add namespace="MyCustomHelpers"/>
    </namespaces>
</pages>

The above just doesn't want to work. I know the namespace is fine because when I put the @using MyCustomHelpers at the top of the page it magically works.

This is so that I can get the Html.SomeFunction() to work without having to put @using at the top of all my pages

marcind
  • 52,944
  • 13
  • 125
  • 111
MyNameIsJob
  • 2,508
  • 2
  • 17
  • 17
  • 1
    which release of MVC 3 are you using? – marcind Oct 06 '10 at 23:14
  • I was using MVC 3 Preview so the other answer worked for me and still works, even after upgrading...however your answer seems more appropriate for the new version - which I will upgrade to shortly. Shame I can't accept two answers. – MyNameIsJob Oct 07 '10 at 14:12
  • It's not clear from comments in the answers, but in MVC4, the namespace needs to be added to the Web.Config file in the \Views folder, NOT the application Web.Config. The `````` config section should already exist in THAT Web.Config! – Ben McIntyre Feb 26 '15 at 02:08

5 Answers5

163

Update: please take a look at my updated answer that applies to MVC 3 RC: Razor HtmlHelper Extensions (or other namespaces for views) Not Found

This has changed between MVC 3 Preview 1 and MVC 3 Beta (released just today). In Preview 1 Razor used the WebForms namespaces config section. However in the Beta there is a new config section that is seperate from the WebForms one. You will need to add the follwing to your web.config file (or just start with a brand new project from the template):

<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>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="MyCustomHelpers" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

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

Note that there are other changes to what is required in web.config to get Razor to work in MVC3 Beta so you would be best off to take a look at the ~\View\Web.config file that ships in the Beta project templates.

Community
  • 1
  • 1
marcind
  • 52,944
  • 13
  • 125
  • 111
  • 2
    FWIW, I've found that the `pageBaseType="System.Web.Mvc.WebViewPage"` attribute is also required. I've updated your answer. – Portman Oct 27 '10 at 14:24
  • 1
    Is this the same way you would do it for MVC 3 RC? – Clark Nov 15 '10 at 16:36
  • 1
    +1 THANK YOU. I spent way too much time trying to find the answer to this problem today. – jessegavin Nov 23 '10 at 20:06
  • 32
    you'll most likely need to close and reopen the .cshtml file if it's not working or giving an error – Simon_Weaver Dec 17 '10 at 05:53
  • 1
    @Simon yes, you need to update reopen the file. The editor right now is not watching for web.config changes. – marcind Dec 17 '10 at 07:08
  • 1
    arg, why is this so damn difficult. I've followed this advice (and every other variation) and I still get nowhere but squiggly lines and compilation errors at runtime. – quentin-starin Jan 31 '11 at 22:21
  • I just tried this and it worked! I had to restart visual studio in order for intellisense to start working. – Razor Mar 28 '11 at 10:43
  • That doesn't work on VS2012. Unable to add this config section. – Eduardo Feb 12 '13 at 23:03
  • Doesn't work in VS2010 with MVC4. It doesn't understand the `` element and fails to process the application. – ygoe Jun 03 '14 at 18:26
  • Update: This must be done in the ~\Views\Web.config file, not ~\Web.config! Then it works. – ygoe Jun 03 '14 at 18:36
50

If you put your namespace declaration in the Web.config in the root "Views" folder and/or the current area's "Views" folder (depending on where your view is) - it should work as expected.

mkaj
  • 3,421
  • 1
  • 31
  • 23
Buildstarted
  • 26,529
  • 10
  • 84
  • 95
6

I found this http://weblogs.asp.net/mikaelsoderstrom/archive/2010/07/30/add-namespaces-with-razor.aspx which explains how to add a custom namespace to all your razor pages.

Basically you can make this

using Microsoft.WebPages.Compilation;
public class PreApplicationStart
{
   public static void InitializeApplication()
   {
       CodeGeneratorSettings.AddGlobalImport("Custom.Namespace");
   }
}

and put the following code in your AssemblyInfo.cs

[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")]

the method InitializeApplication will be executed before Application_Start in global.asax

k-dev
  • 1,657
  • 19
  • 30
  • Method moved to System.Web.WebPages.Razor.WebCodeRazorHost.AddGlobalImport("Fully.Qualified.Namespace"); – agrath Jul 15 '17 at 00:32
4

For what it's worth, another technique is to simply put your helper extension class in a System namespace that is already included by the view engine by default. For example:

namespace System.Web.Mvc
{
    public static class HtmlHelper_MyExtensions
    {
        ...
    }
}

This way, as long as the library containing the class is referenced, everything will see it. Since you are extending a System.Web.Mvc class, it seems reasonably acceptable to put the extension methods in the same namespace.

Peter
  • 1,488
  • 2
  • 17
  • 23
0

Try closing and reopening the view after making sure the changes were made to the web.config in the root of view.

Closing and Reopening fixed my problem.

See here:

ASP.NET MVC 4 namespace issue in razor view

Community
  • 1
  • 1
eaglei22
  • 2,589
  • 1
  • 38
  • 53