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.
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.
In Solution Explorer, right-click your Web site name, and then click Add New Item. In the Templates window, click Web Configuration File.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With