Shared project templates and ASP.NET Empty Web Site| Comments
If you are like me, you probably create a lot of projects in Visual Studio that end up getting thrown away and are intended just to test out a theory you have, double-check yourself when you are going mad because you can't find a bug, testing out something you read on a blog post, whatever.
Most of the projects I create are web projects. I've started to adopt the "_Delete" mechanism to help me identify what I can truly delete in my folders later. But is not the point here. One thing that I do when creating web projects is use the Empty Web Site template:
I choose this template because it is the cleanest...it's actually what it says it is: empty. It allows me to really isolate things as I'm the one adding things in, not the IDE. So I usually follow that up with a new WebForm to get an ASP.NET page in there. I write some code then CTRL+F5 it to run. The first thing I hit is a build error. Can you guess what it is?
Here's the thing. When you choose the Empty Web Site template, you literally get nothing...no web.config, no default pages, no references, nada. Like I said, it is what it is: empty. But herein lies the problem. The WebForm item template doesn't know that. The WebForm item template is shared with all the web template types, which generally would be fine. Let's do some digging to find out why I get a build error.
The WebForm item template is located in %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\1033\WebForm.zip. This file contains the WebForm.aspx and code beside templates. If you look at the CodeBeside.cs template within there you will see this line:
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
And there is the problem! When I create a new web site, i'm using the .NET Framework 3.5 setting:
instead of any other target. It makes sense because I'm doing 3.5 stuff anyway and I don't want to have to change it later. But the problem is that the WebForm template now thinks that I already have web.config settings for LINQ, a reference, etc. and that is why I can't run...no references to System.Linq assemblies anywhere.
It's quite annoying for me, but I would imagine not most. In talking with some product team folks, and I concur, people who choose the empty template are usually likely going to know what is up and make whatever changes needed. Fair enough, but still slightly annoying only because I keep forgetting about it!
So since I find myself removing those using statements a lot, I decided to just change the template. This, of course, is completely going to get overwritten any update in the future. But for me, I think it made sense to remove them rather than to assume that I, the developer, will always be using LINQ in my web forms...which I'm not. I'll choose to add it back in when I need! So I simply removed the $if$ statements and I'm good to go. I wish there was a way to say $if$ (3.5 && !empty)using System.Linq, but I'm okay with my minor change for me.
Anyhow, I thought I'd share this useless piece of knowledge as you might have come across it while checking out new development, tinkering with ASP.NET, creating shell Silverlight web hosts, etc. Hope it helps.
Please enjoy some of these other recent posts...