1

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

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

3 Answers3

2

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.

Ocelot20
  • 10,510
  • 11
  • 55
  • 96
2

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

Max Toro
  • 28,282
  • 11
  • 76
  • 114
  • Nope. This doesn't work. Check my **edit** section of the question where I clarified things a bit more. – Robert Koritnik Nov 11 '10 at 19:54
  • @Robert Koritnik: Cannot reproduce the issue, what is the error message? – Max Toro Nov 11 '10 at 22:16
  • No error. Linq extension methods just don't show up in intellisense dropdown... Even if I write them down manually, they are curvy-red underlined meaning Visual Studio doesn't recognise them. If I do add `imports` or even add all those elements as I described in the other question (referenced above in this question) there's no difference. – Robert Koritnik Nov 11 '10 at 23:46
  • @Max Toro: I've added steps to reproducing the problem above. – Robert Koritnik Nov 11 '10 at 23:54
  • @Robert Koritnik: Followed steps and everything works fine, adding System.Linq to web.cofig/Import directive enables intellisense. – Max Toro Nov 12 '10 at 02:07
  • @Max Toro: I added my complete root web.config file + screen capture of the unrecognised Count extension method in my Asp.net MVC view. Can you check out web.config where it differs from yours? – Robert Koritnik Nov 12 '10 at 09:18
  • 2
    @Robert Koritnik: You need System.Core for Linq, why is that commented-out? – Max Toro Nov 12 '10 at 13:57
  • @Max: In the other question I've said there's no difference if I do include `System.Core` (I've added the same info here as well). And if you create a new Asp.net MVC 2 project, `System.Core` is not included in the `web.config` either. It's part of upper hierarchy configuration anyway (I suppose, but I didn't check). – Robert Koritnik Nov 14 '10 at 09:43
0

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>
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Right. Interesting that your project compiled without it. BTW, if you change the target framework on the Properties page VS will add this for you. – Max Toro Nov 15 '10 at 16:28
  • It does yes. But as mentioned I've cleaned up my `web.config`, because my application is not a standalone web application but is running under an existing Sharepoint website (just as a virtual folder, not as a virtual folder application). So I had to remove a lot out of my `web.config` and keep just the relevant parts. Why wouldn't it compile? Everything that was problematic was part of views, which are by default not being compiled until runtime. And since I've had these settings in the site's root `web.config`, I wasn't getting any runtime errors either. – Robert Koritnik Nov 15 '10 at 18:08