在全球添加“< @ Assembly>”和“< @Import命名空间>”到Asp.net MVC的看法

时间:2010-10-06 10:12:13

标签: asp.net-mvc configuration

而不是写

<@ Import Namespace="Microsoft.SharePoint" %>

在我创建的每个视图上,我知道我可以轻松编辑我的web.config文件并添加此文件:

...
<pages>
    <namespaces>
        <add namespace="Microsoft.SharePoint" />
    </namespaces>
</pages>

但这似乎在设计时并不奏效。除非我在视图上添加这两行,否则Visual Studio 2010无法看到SPContext

<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>

那么如何全局添加程序集以及导入名称空间,以便VS能够解析类/对象?

2 个答案:

答案 0 :(得分:5)

您还需要将程序集添加到<assemblies><compilation>部分的<system.web>部分:

<compilation debug="false" targetFramework="4.0">
  <assemblies>
    <add 
      assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  </assemblies>
</compilation>

您的targetFrameworkdebug属性值可能会因您要定位的框架版本以及是否正在调试而有所不同。

答案 1 :(得分:2)

如果你也在Web.config中添加程序集,它应该可以工作。