Robert Koritnik
Robert Koritnik

Reputation: 105029

Visual Studio 2010, Linq extension methods and framework 3.5

If I set <compilation targetFramework="4.0"> in web.config, Visual Studio 2010 shows all Linq extension methods in ASPX files (not codebehinds). But when I change my project to target 3.5 (which supports Linq extension methods) Visual Studio removes the previously mentioned attribute in web.config, but Linq intellisense in APSX files goes with it as well.

Question

Is it possible to convince Visual Studio 2010 to not assume and fall back to 2.0 when editing ASPX files, so Linq extension methods would still be listed in intellisense dropdown?

Edit

Manually adding assemblies and import namespaces doesn't do the trick as I've pointed out in one of my previous questions, when I didn't know what was going on.

Problem reproduction

To reproduce this issue do the following:

  1. In Visual Studio 2010 open Asp.net MVC project properties and target NetFx 3.5
  2. Open web.config and remove targetFramework attribute if it's still there.
  3. Write some code in the view itself (ASPX) that uses Linq extension method (ie. (new List<string>()).Any(s => string.IsNullOrEmpty())). You should see that Any is not recognised by Visual Studio 2010.
  4. Start adding one configuration setting by one in web.config. There should be no difference about Any method.
  5. Add <% @ Imports ... %> to the view. There should be no difference about Any method either.

Running the application is not a problem. It runs and it also executes Any Linq extension method. No problem with that. Design time support is the issue here.

My web.config file

This is the complete content of my web.config file, that doesn't do the expected (I can include commented out parts as well, but that doesn't make any difference - original Asp.net MVC 2 project template doesn't include these either):

<?xml version="1.0"?>
<configuration>

    <system.web>

        <httpHandlers>
            <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>

        <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">
            <controls>
                <add tagPrefix="SharePoint" assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls" />
            </controls>
            <namespaces>
                <add namespace="Microsoft.SharePoint"/>
                <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>

    </system.web>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
            <remove name="BlockDirectAccessHandler"/>
            <add name="BlockDirectAccessHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>

</configuration>

This is how it looks like in Visual Studio 2010. In the image you can't see the extra line <%@ Import Namespace="System.Linq" %> that should be right after <%@ Control ... %>, but I've tried with and without. system.web/pages/namespaces is the global setting for this anyway.

alt text

Upvotes: 1

Views: 1274

Answers (3)

Robert Koritnik
Robert Koritnik

Reputation: 105029

Including System.Core assembly is of course important. But doesn't do anything is you don't also do this next thing.

To make everything work in Visual Studio 2010 as expected (and under framework 3.5) you have to add <system.codedom> configuration element in web.config as well. This is the part that was missing:

<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>

Upvotes: 0

Ocelot20
Ocelot20

Reputation: 10800

I think it's because LINQ is assumed to be included in the 4.0 framework. For older versions, you can manually import the LINQ namespace by adding this to each ASPX page at the top:

<%@ Import Namespace="System.Linq" %>

Or if you don't want to do that in each file, you can put it in your web.config.

Edit - As has been pointed out by others, your problem may come from the fact that you have System.Core commented out of your web.config, which is required for those extension methods.

Upvotes: 2

Max Toro
Max Toro

Reputation: 28608

The root web.config for .NET 4.0 has System.Linq added to system.web/pages/namespaces. This is not the case for .NET 3.5 which, although it includes the Linq library, did not introduce changes to the root web.config for backwards-compatibility reasons, so you have to add it to the system.web/pages/namespaces in your web.config.

See ASP.NET Configuration File Hierarchy and Inheritance

Upvotes: 2

Related Questions