Today I noticed that e.g. "System" and "System.Web.Security" are imported in all my razor views, although I did not actively import them. I checked:
- @using directive
- web.config (EDIT: also global web.config)
- AddGlobalImport
EDIT:
It seems these namespace imports are hardwired in the Razor Source Code
private WebPageRazorHost()
{
NamespaceImports.Add("System");
NamespaceImports.Add("System.Collections.Generic");
NamespaceImports.Add("System.IO");
NamespaceImports.Add("System.Linq");
NamespaceImports.Add("System.Net");
NamespaceImports.Add("System.Web");
NamespaceImports.Add("System.Web.Helpers");
NamespaceImports.Add("System.Web.Security");
NamespaceImports.Add("System.Web.UI");
NamespaceImports.Add("System.Web.WebPages");
NamespaceImports.Add("System.Web.WebPages.Html");
// ...
}
It also seems that I can't disable these by setting a "clear" at the beginning of my web.config:
<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>
<clear />
<add namespace="System.Web.Helpers" />
<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" />
</namespaces>
</pages>
</system.web.webPages.razor>
So this issue might be unfixable unless Razor source is changed.
So the answer is: In Razor views some namespaces are always imported. This can't be disabled in configuration because it is hardwired in the code. These namespaces are:
I am not 100% sure that this is the complete answer, but as long as there is no opposing comment I will accept it.
it is common , because it is registered in overall web config file at following location :
it is registered like this :
EDIT:
following is from an SO answer here:
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):
I ran across this question while answering another one (Removing System.Web.UI from a Razor view). Below is a copy of my answer. It may be useful.
I'm not sure if this is what you're after, but I did a quick experiment. I think it works.
I extended the MvcWebRazorHostFactory a bit. The new factory basically passes along the host created by the base class, but removes the namespace first.
This is the class:
Then I changed web.config to use my factory instead of the standard one:
Bingo!