3

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.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404

3 Answers3

5

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.

amurra
  • 15,221
  • 4
  • 70
  • 87
  • Adding this attribute makes things fine in the editor. But the thing is I'm targeting 3.5. So I get runtime error that this attribute isn't recognised. – Robert Koritnik Nov 11 '10 at 18:18
1

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)

Manatherin
  • 4,169
  • 5
  • 36
  • 52
  • Nope... Tried that already... Also `<@ Assembly Name="System.Core" %>` with it... No difference. I'll update my question to point out I've tried that as well. – Robert Koritnik Nov 11 '10 at 11:06
1

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>
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404