During everyday Sharepoint development we often use application layout pages (aspx pages which are located in 12/Templates/Layouts folder on file system of web server) for tracing and debugging. It is useful technique, as you can include server-side script directly into the page and show results on the page in context of the living site. Suppose that we have sub folder 12/Templates/Layouts/Test and we created simple aspx page here with the following code:
Look at the code inside <%%>. It is very simple example: it just assigns integer value to variable “i” and shows it in the label. In real life you will execute some meaningful code and trace its result in the label or in another control (e.g. display current user identity).
However it is annoying that by default you are limited with C# 2.0 features, and can’t use features available in C# 3.0 (see Overview of C# 3.0 for full list). E.g. if instead of “int i = 1;” you will write “var i = 1;” (i.e. if you will use Implicitly Typed Local Variables), then you will get the following error when you will try to access your page:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\test\test.aspx(9): error CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Recently my colleague showed me the tip how to avoid this issue (unfortunately he doesn’t blogging so he is not able to publish it by himself). So in order to enable C# 3.0 features in application layout pages you should create the following web.config file in your sub folder (12/Templates/Layouts/Test in our case):
This web.config overrides compiler version (see <providerOption name="CompilerVersion" value="v3.5"/>) used by ASP.Net, so you will be able to write C# 3.0 code in your pages after that. It helped us to write application layouts page more effectively and I hope it will simplify your everyday Sharepoint development and debugging too.