Reputation: 68440
I have an extension method on HttpRequest that I'm trying to use inline on different aspx pages.
Despite I've added the correct import to the aspx I get an error saying that the method doesn't exist. The extension method works perfect on code behind.
Compiler Error Message: CS0117: 'System.Web.HttpRequest' does not contain a definition for...
In order to fix this, I had to add this web.config declaration
<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>
I found that here but I have no idea why I need this declaration.
I'm not sure what's for and I'm concerned that it might have some side effect.
Do you know why I need this declaration and how I could avoid it?
Upvotes: 2
Views: 363
Reputation: 107566
Extension methods were introduced in .NET 3.5. By default, inline code is compiled with CompilerVersion v2.0. You need to add the block you specified to instruct the runtime to compile language features introduced in .NET 3.5, hence the CompilerVersion value of "v3.5".
The problem is specific to ASP.NET, as extension methods compile in normal applications under the CLR 2.0 compiler.
Upvotes: 2