Visual Studio doesn't show Linq extensions in my Asp.net MVC views

大城市里の小女人 提交于 2019-12-02 03:06:22

问题


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.


回答1:


You need to add the attribute of targetFramework to the compilation element in your web.config:

<system.web>
    <compilation debug="true" targetFramework="4.0">

Add that to your web.config and you should regain intellisense again in your views.




回答2:


could try

<%@ Import Namespace="*" %>

where * is the namespace you want to use e.g. System.Linq

probably not the best way to do but it may work (not at a computer with vs so cant test)




回答3:


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>


来源:https://stackoverflow.com/questions/4153504/visual-studio-doesnt-show-linq-extensions-in-my-asp-net-mvc-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!