Visual Studio不在我的Asp.net MVC视图中显示Linq扩展 [英] Visual Studio doesn't show Linq extensions in my Asp.net MVC views

查看:1821
本文介绍了Visual Studio不在我的Asp.net MVC视图中显示Linq扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我感到困惑,我似乎不能使Visual Studio 2010识别System.Linq扩展方法在视图代码。 Intellisense不工作,Visual Studio红色底线是无法识别的扩展方法。



这些是web.config最相关的部分,我认为是与Visual Studio相关的。 Linq扩展方法。注释掉的行可能未注释,但没有区别。

 < compilation debug =truebatch =true> 
< assemblies>
<! -
< add assembly =System.Core,Version = 3.5.0.0,Culture = neutral,PublicKeyToken = B77A5C561934E089/>
< add assembly =System.Xml.Linq,Version = 3.5.0.0,Culture = neutral,PublicKeyToken = B77A5C561934E089/>
- >
< add assembly =System.Web.Abstractions,Version = 3.5.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35/>
< add assembly =System.Web.Routing,Version = 3.5.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35/>
< add assembly =System.Web.Mvc,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35/>
< / assemblies>
< / compilation>

< pages enableViewState =false>
< namespaces>
< add namespace =System.Collections.Generic/>
< add namespace =System.Linq/>
< add namespace =System.Web.Mvc/>
< add namespace =System.Web.Mvc.Html/>
< add namespace =System.Web.Routing/>
< add namespace =MyApp.Objects/>
< add namespace =MyApp.Web.General/>
< add namespace =MyApp.Web.Helpers/>
< / namespaces>
< / pages>

我有一个部分视图定义,看起来像这样。同样的是注释掉两行。取消注释无差别:

 <%@ Control Language =C#Inherits =System.Web.Mvc.ViewUserControl< IList< ToolbarItem>> %> 
<% -
<%@ Assembly Name =System.Core%>
<%@ Import Namespace =System.Linq%>
- %>
<%
if(!this.Model.Any(ti => ti is ToolbarText&&(ti as ToolbarText).MaximizeWidth))
{
.Model.Add(new ToolbarText {MaximizeWidth = true});
}
%>

在此特定局部视图中 Any()即使在 System.Linq 命名空间下的 System.Core 程序集中定义了扩展方法,

我缺少哪些配置设置?看起来Visual Studio看不到 System.Core 程序集以枚举它在 System.Linq 命名空间中的扩展方法。

解决方案



但是当我正在清理我的 web.config 文件我删除了这一部分,这是必须的(显然)对Visual Studio 2010,使事情按预期工作。

 < system.codedom> 
< compilers>
< compiler language =c#; cs; csharpextension =。cstype =Microsoft.CSharp.CSharpCodeProvider,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089warningLevel = 4>
< providerOption name =CompilerVersionvalue =v3.5/>
< providerOption name =WarnAsErrorvalue =false/>
< / compiler>
< / compilers>
< /system.codedom>


This baffles me and I can't seem to make Visual Studio 2010 to recognise System.Linq extension methods in view code. Intellisense doesn't work and Visual Studio red underlines unrecognised extension methods.

These are web.config's most relevant parts, that I think are related to Visual Studio to recognize System.Linq extension methods. Commented out lines may be uncommented but there's no difference.

<compilation debug="true" batch="true">
    <assemblies>
        <!--
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        -->
        <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
</compilation>

<pages enableViewState="false">
    <namespaces>
        <add namespace="System.Collections.Generic"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="MyApp.Objects"/>
        <add namespace="MyApp.Web.General"/>
        <add namespace="MyApp.Web.Helpers"/>
    </namespaces>
</pages>

I have a partial view definition that looks like this. The same about commented out two lines. Uncommented make no difference:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<ToolbarItem>>" %>
<%--
<%@ Assembly Name="System.Core" %>
<%@ Import Namespace="System.Linq" %>
--%>
<%
    if (!this.Model.Any(ti => ti is ToolbarText && (ti as ToolbarText).MaximizeWidth))
    {
        this.Model.Add(new ToolbarText { MaximizeWidth = true });
    }
%>

In this particular partial view Any() extension method is not recognised even though it's defined in System.Core assembly under System.Linq namespace.

Which config settings am I missing? Seems that Visual Studio can't see System.Core assembly to enumerate it's extension methods in System.Linq namespace.

解决方案

Of course System.Core assembly has to be included in the configuration compilation.

But when I was cleaning up my web.config file I deleted this part, that is imperative (obviously) to Visual Studio 2010 to make things work as expected.

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
            <providerOption name="CompilerVersion" value="v3.5"/>
            <providerOption name="WarnAsError" value="false"/>
        </compiler>
    </compilers>
</system.codedom>

这篇关于Visual Studio不在我的Asp.net MVC视图中显示Linq扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆