Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add my project namespace to MVC web.config

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.

like image 966
Water Cooler v2 Avatar asked Dec 31 '12 11:12

Water Cooler v2


People also ask

How to add Web config file in ASP net MVC?

A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code.

How to add Web config in c#?

In Solution Explorer, right-click your Web site name, and then click Add New Item. In the Templates window, click Web Configuration File.

What are the config files in MVC?

config file in MVC application is managed by the NuGet which will keep track of what packages and versions have installed within your application. The Web. config file of an MVC application is one of the most useful and important files which contains the application level configurations.

What is namespace MVC?

Mvc namespace contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial view, model binders, and much more.


2 Answers

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.

like image 101
DVK Avatar answered Oct 12 '22 19:10

DVK


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.

like image 26
danludwig Avatar answered Oct 12 '22 19:10

danludwig