2

There are many examples of adding a using statement for all views in web.config for prior versions, but I have not found one for MVC 5.

Can someone give me an example?

Greg Gum
  • 29,500
  • 31
  • 143
  • 205
  • possible duplicate of [How to add extra namespaces to Razor pages instead of @using declaration?](http://stackoverflow.com/questions/3875207/how-to-add-extra-namespaces-to-razor-pages-instead-of-using-declaration) – Ufuk Hacıoğulları Mar 13 '14 at 14:48
  • That answer shows adding a config section to the root config which is different from the answer below. – Greg Gum Mar 13 '14 at 14:56

2 Answers2

9

Locate Web.config in the Views folder.

Find the namespace section. It will already have namespaces for Mvc.

Add the desired namespace:

<namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="MyNamespace" />
</namespaces>
jrummell
  • 41,960
  • 17
  • 114
  • 168
Greg Gum
  • 29,500
  • 31
  • 143
  • 205
  • 2
    How was it different from the other versions? – Ufuk Hacıoğulları Mar 13 '14 at 14:44
  • No example I could find said to put it in the Web.config file under Views, as opposed to the root Web.config. – Greg Gum Mar 13 '14 at 14:53
  • @GregHollywood - most examples I can find do mention the views/web.config - http://stackoverflow.com/questions/4136703/razor-htmlhelper-extensions-or-other-namespaces-for-views-not-found/4136773#4136773 :) They may not always be so clear on the location, but they do mention it – Tommy Mar 13 '14 at 15:06
  • 1
    OK, well I tried several of those answer to no avail (missing the fact) and finally figured it out, so I thought I would call it out here for the next guy! – Greg Gum Mar 13 '14 at 15:10
1

For ASP.NET Core MVC you can also add inside _ViewImports.cshtml a statement such as this at the top if you needed to add the models class to your views.

@using Myprojectnamespace.Models
Dale K
  • 21,987
  • 13
  • 41
  • 69
Mario Levesque
  • 956
  • 12
  • 9