20

Can I not add my project namespace to the web.config so that the Razor view engine includes my project namespace for all pages in my project? Like so:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
        <add namespace="System.Web.Routing" />

        <add namespace = "MyProjectNamespace.NestedNamespace"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

It isn't working.

1
  • Looks okay to me. By "It isn't working" do you mean you don't have intellisense or that the page loads with an error? Have you tried rebuilding the application? Commented Dec 31, 2012 at 11:40

3 Answers 3

36

This is an old question, but apparently there is still a bit of a problem with Visual Studio (2015 2017 2019 as of right now). After adding the namespaces to the web.config file in your Views folder, close and re-open all views. The namespaces were not recognized and the views would continuously throw runtime errors until I closed them all.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for specifically pointing out VS2015. I was sitting here, pounding my head thinking...I know this is how you do it, wtf?
GOTT dangit! I spent 30 minutes trying to figure this crap out! GRRR!!!
Saved my life. Also as @Hill I spent lot of time trying different things. Hope MS solve this issue with Update 4 of VS2015. Thanks a lot @DVK!
Someone should sue Microsoft for this
Just a note to say this is, somehow, still an issue in VS2017.
|
29

The namespace needs to be added to the web.config file in your Views folder, not the root project's web.config. This will import the namespace for all cshtml files in that views folder only. If your project has MVC areas, each area will have its own Views folder and a separate views web.config file. To import the namespace into those views, you need to add the namespace to each area's view folder's web.config as well.

2 Comments

Just to emphesise (as it tripped me up) the root of the views, not in any subfolders of the Views folder! Also make sure the resource file is public.
Close and reopen you view after the web.config change.
-4

You can also add a @using statement to a _ViewStart.cshtml file which will cover all views in the same folder or any subfolders.

@using MyProjectNamespace.NestedNamespace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.