问题描述
今天我注意到,例如系统和System.Web.Security都是进口的在我所有的剃须刀的意见,虽然我没有积极地导入。我查了一下:
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指令
- 的web.config(编辑:也是全局的web.config)
- AddGlobalImport
编辑:
看来这些命名进口在剃刀来源$ C $ C硬
It seems these namespace imports are hardwired in the Razor Source Code
https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.WebPages.Razor/WebPageRazorHost.cs
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");
// ...
}
这也似乎我不能在我的web.config的开头设置一个明确的禁用这些:
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.
推荐答案
因此,答案是:在剃刀认为一些命名空间始终会导入。这不能在配置,因为它是在code的硬连线被禁用。这些命名空间是:
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:
- 系统
- System.Collections.Generic
- System.IO
- System.Linq的
- System.Net
- 的System.Web
- System.Web.Helpers
- System.Web.Security
- 的System.Web.UI
- System.Web.WebPages
- System.Web.WebPages.Html
我不是100%肯定这是一个完整的答案,但只要没有反对意见,我会接受它。
I am not 100% sure that this is the complete answer, but as long as there is no opposing comment I will accept it.
这篇关于奇怪的自动命名进口我剃刀意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!