Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

From ASP Stock Projects to Web Application Projects in VS 2008


:P
On this page:

As I’m working on some of my demos for DevConnections next week I decided that some older demos I’ve had should be moved to Web Application Projects (WAP) rather than running on stock projects for consistency. I tend to use only WAP projects in production, but for demos I’ve often stuck with stock projects because they worked with Visual Web Developer Express. Now that SP1 has shipped though even VWD supports WAP projects (a big thanks for the ASP.NET team to making that happen!) and so there’s no need to use the stock project model any longer.

Other than consistency with other projects, there was also the issue of WCF Web Services behaving badly under stock projects. For whatever reason I still can’t figure out with stock projects WCF Services often simply stop working with a compilation error even though there have been no code changes. It appears the problem is related to the temporary file storage in the ASP.NET temp folder with copies of the App_Code assemblies getting out of sync with the service in some  way. There’s a slight fix for this in stock projects by turning batch compilation off:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" batch="false">
        </compilation
>
    </system.web>

</configuration>

This has helped make the problem less frequent, but it still occurs from time to time.  Using Web Application Projects this is not an issue because the full compile causes everything to be compiled into the same assembly so there’s no real chance of a version mismatch.

Anyway, so I decided I need to move this rather large demo project that contains many diverse examples from a stock to WAP project and as it turns out it took quite a bit of work to do it.

Converting from Stock to WAP in VS 2008

Prior to VS 2008 stock projects had an option to allow you to convert projects to WAP projects via a menu option – Convert to Web Application Project. That option is no longer available so the process has become fully manual.

Here are the steps that you should go through followed by a few gotchas you need to watch out for:

  • Open your Stock project in a Visual Studio Solution
  • Add a new Web Application Project to the Solution
  • Drag and Drop all files from the Stock project into the WAP Project
  • Remove the BIN folder (or don’t copy it in the first place)
  • Manually add back ALL assembly/project references to the WAP Project
    It’s best to do this first to make sure that related files are found and any auto-compiled components (like Linq to SQL models or Web References) get properly properly translated when files are converted in the next step.
  • Right click on the root node of the project and Convert To Web Application
  • Rename APP_Code to something else if the conversion didn’t do it for you. I use Classes as my ‘code only’ folder
    APP_Code is an illegal folder name for a WAP project – if you don’t rename you’d end up with duplicate compiled classes which will cause problems. Ideally you shouldn’t have much code here – it’s best to put this sort of stuff into a separate project. Note that you can move any CodeBehind files (like Web Service, Handler or other ‘known ASP.NET files’) back into the actual content folders – no need to keep them in the App_Code/Classes folder since they’re easier to find with the actual content files.
    Note that Convert to Web Application should rename the APP_CODE folder and it did the first time I did this. Just now when I walked through this one more time to verify for writing it up however it didn’t create an OLD_APP_CODE folder. Further I couldn’t rename the folder at first. I ended up explicitly running Convert to Web Application on the old APP_CODE folder. After that I was able to rename it.

  • IMPORTANT: Select all files that were in the old APP_CODE folder and set their Build Action to Compile.
    Files in the APP_CODE folder in stock projects are compiled by ASP.NET rather than Visual Studio so the default action for APP_CODE files in stock projects is content which doesn’t compile. When moved to a WAP project that same action is passed forward which means that Visual Studio is not compiling APP_CODE files by default. Make sure you switch the Build Action to Compile or else you’ll end up not finding the classes when compiling your project. This one threw me for a loop because I got compiler errors pointing at classes that I knew were in the project.
  • Compile your WAP Project – rinse wash and retry
    There are bound to be a few compilation errors in your project when you do this. Pay especial attention to missing classes (check that the classes in question are set to Compile not as Content) or missing namespaces (make sure that classes other than pages have a namespace). Also make sure that you have all necessary assemblies referenced. You’ll likely get a bunch of errors and go through this cycle of compiling fixing and a host of errors until all references and classes are properly added. Read the next two bullets for a few more hints on possible compilation issues.
  • Remove and Reattach Web and Service References 
    The Conversion imports Web and Service references but it attaches them into a separate folder. For me the Web References didn’t work correctly so instead I ended up removing them and re-adding them using the standard Web Reference/Service Reference tools. Note that in WAP this will cause Namespace changes – from only the name you assigned in Stock projects to the Project’s default namespace + the name you assigned. IOW, you’ll have a little bit of refactoring to do to fix these services.
  • Linq To Sql Models
    My project had two Linq to SQL models and while both of the models transferred, they didn’t immediately compile. The problem is that the old designer file is detached and a new attached and somewhere in that process the actual class is not visible so that any reference to the model fails.

    LinqToSql
    The solution for me was to go delete both of the code behind files (the .designer files) both in the project and from disk. Then going into the model and resaving it. You’ll end up with the TimeTrakker1.designer.cs file which is odd, but works properly. The key is to make sure that the original file is deleted and removed.

So as you can see doing a WAP conversion is by no means a trivial task. I ended up spending a good two hours converting this project by the time I had everything back up and running as it did before. And that’s having done this before and having some idea what to look out for. The hardest part for me were the auto-building files – Web Services and the LINQ to SQL Models that just didn’t want to work after initial conversion.

Part of the problem was that the compiler wasn’t being helpful. I had the L2S model in the project for example with proper namespaces set on the model, yet the compiler failed to see the actual model’s classes. No error during model generation – I could see the classes in the model’s codebehind but the compiler failed to see the classes. When this happens the thing to check is to make sure that the Build Action is set properly and/or that there isn’t another file that is interfering with the namespace resolution.

The good news is that the issues are all related to file location/switch settings and once those were resolved the project just kicked right in and worked as expected. No further tweaking required. Personally I much prefer WAP’s more formal compilation approach and the reliability that comes from having a single code behind assembly that knows where are code behind code lives in one place instead of scattered through multiple temp assemblies as is the case with Stock projects.

I have a couple of other projects that I should do this to as well – not sure if I’m willing to put in the time again though. <shrug>

Posted in ASP.NET  

The Voices of Reason


 

Stacy
November 06, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Did you have to convert the Profile object and if so how did you do it?

Rob Conery
November 06, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Are people still using WAP's? Dude isn't that like... 8 months ago or something?

I am, of course, joking. Sort of.

Go get in the water!

Pilotbob
November 06, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Rick,

We tend to use WAP projects too. But mostly because I haven't had time to convert them to Web Site projects. With the WAP projects you loose the auto profile class stuff... so you have to generate and maintain a profile class manually. Thats not to bad though.

The worst part about the WAP is that you need to set up an IIS virtual to support it. This is not great for a project that you want to branch in source control. Even having the dev create an IIS virtual isn't to bad. However, the virtual path is stored in the .csproj/.vbproj. This to me is the most stupid design of all. This requires all devs to use the same virtual for each branch. Also, the project file needs to be updated when you branch... and you need to send an email or something to everyone to tell them what virtual to use for the new branch.

It seems to me that this information should be stored in the .user file the way the reference paths are. This would allow for each dev to set up any virtual they want. Since the .user files are not put into source control when you open a WAP that doesn't have one, it should prompt you for it and add it to the .user file.

Can you tell me, does VS 2008 by any change solve this? If not, I am seriously wanting to move my stuff FROM WAP to Web Site.

Thanks,
BOb

Tony Bunce
November 06, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

I agree with you Rick, WAPs are way better than web site projects. Web site projects are almost unusuable.

@Bob

WAPs work fine with the the developer web server, IIS is not required at all.

In VS2008 there is an option to store the settings in the project file or in the user file. I think it defaults to the user file.

Guy Harwood
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

The 'stock' website projects are simply awful, we always use WAP.

Danijel
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Web application site is a way better than Web site projects. Web sites projects should only be used as demo in my opinion.

Derek Morrison
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

@Stacy

Although Web Application Projects don't automatically generate a strongly-typed Profile class, there are several work arounds such as a using custom build task (http://code.msdn.microsoft.com/WebProfileBuilder), hand coding a Profile class yourself, or (my preferred way because of its simplicity) just using the non strongly-typed methods provided by the Profile classes (http://blog.stevienova.com/2008/03/01/visual-studio-20052008-web-application-projects-profile-class-auto-generation-workaround/).

PilotBob
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

@Tony WAPs work fine with the the developer web server, IIS is not required at all

DOH! I didn't even think of that or maybe I didn't and can't remember why we don't do it. Are there things that work differently in IIS than Cassini?

Another issue though is that we have multiple web app projects that need to be set up as virtual directories (not apps) under our main folder to get this to work. But, I have been wanting to drop those projects and just make them folders of the main project.

BOb

Rick Strahl
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

@Bob - you can create a virtual anywhere - it doesn't need to be under the Web root. I have nothing dev under my actual Web root folder on disk for example and all web projects including Web apps live under my Dev root.

One thing you may have to do in that case though is add permissions for the IUSR_ account to the folder so that you get anonymous access to work.

martin
November 07, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

What's the advantage of converting a web site project to WAP?
Thanks!

Ragnar Österlund
November 10, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Doesn't anyone experience enormously long loading times when debugging when the project gets larger and larger with WAP. I prefer the Web Site Project model because it permits changing a page's CodeFile without reloading the symbols of the whole project DLL. As far as I have seen, there is no workaround for this with WAP. The tiniest change in some page or UserControl will result in 30 sec loading of symbols on a rather fast machine. Correct me if I am wrong.

/Ragnar

Rick Strahl
November 11, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

@Ragner - that is true, but what are you doing to have such large amounts of code in your Web project? Are you putting your business logic in there? If you break out your business logic into separate projects there shouldn't be that much code in Codebehind?

I have some fairly large projects and they load no slower than stock projects...

Ragnar Österlund
November 11, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Rick, we have 3 large class libraries that our project depend upon. A lot of logic is in those libraries. Some are in the App_Code folder of our website. If I change in these places, the load time is equal to that of a WAP, typically half a minute. And if I just do a small code change this is really a pain. But if I only change in the code behind, then the load time will be a couple of seconds. So when I develop new logic, I can do it in the codefile, debug and test enjoying fast load times, and when done I can move the code to a classlib or the App_Code. When using the debugger as much as I do, this is the best alternative. I know there is the Edit and Continue feature but it does not support adding new methods.

/Ragnar

Saravana Prakash
December 04, 2008

# re: From ASP Stock Projects to Web Application Projects in VS 2008

Can we move any aspx and ascx files into App_code folder?

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024