posts - 289, comments - 120, trackbacks - 110

My Links

News

My main blog url now is: weblogs.asp.net/meligy

Tag Cloud

Article Categories

Archives

Post Categories

BlogRoll

About Me:

Converting VS 2008 Website to Web Application

Background (skip if you know Web Application Projects)

In VS 2002/2003, the web project model for a website was similar to "class library" projects, where you have a .CSPROJ or .VBPROJ file that keeps track of files "included" in the project, and compiles all the pages and controls code behind to a single assembly under "\bin". Each page/control has an automatically generated .DESIGNER.CS or .DESIGNER.VB file, which contains objects mapping to the server controls in the page/control markup (the generation of those files was not always in synch with markup, and that was problematic).

With VS 2005, there was a new "website" model for web projects that compiles each page/control individually as a separate assembly (or each folder, depending on optimization features), and applies this to all files in a given directory and its sub folders. This was a total mess in most "real world" projects, as VS takes so long to build the entire website, and even at deployment, you get sometimes many problems when you have pages that "reference" other pages/controls when IIS it trying to dynamically load the right assemblies to reference, and many other problems.

So, Microsoft came with a new add in to VS 2005 called "Web Application Projects". This is typically the same old VS 2002/2003 project model with no problems in generating DESIGNER files and with integration with both IIS and ASP.NET development server that comes embedded in VS 2005/2008. It was later merged with VS 2005 SP1, and shipped as part of VS 2008 (without removing the "website" model). Note that most stuff that has to do with Microsoft like ASP.NET AJAX Toolkit Sample website and so are actually "web applications" not "websites".

The problem

Typically, when you are converting any project from VS 2003 to VS 2005 SP1, it converts as "web application" not "website". You can also convert a "website" to a "web application". There's an option "Convert to web application" to look for.

In my company, all our web projects are "web applications", well, except that other web project I was code reviewing and helping with its deployment! After spending number of days with the brilliant team and not finding as many items to code review and getting sick of some problems at sometime in deployment, I cried to them to convert it to to "web application" (maybe I was looking for some job to be doing :D). Very confidently, I said, " remember the option exists and I did conversion before in VS 2005. All it takes is a right click on the 'website' root node in solution explorer in VS 2008 and 'Convert to web application'. It almost never causes any problems, and we have our source control anyway".

They believed they had time to do it, so, they went to look for that menu item  "Convert to web application" and guess what ? They didn't find it! They tried resetting VS 2008 settings and everything, and still, nothing there!!! Yeah, it was embarrassing :D :D :D

Workaround, or, how to convert a "website" to "web application" in VS 2008

Well, it turns out that the option "Convert to web application" does NOT exist for "websites". The option "Convert to web application" does exist only for "web applications" !!!!

So, here's the deal, to do the conversion, you need to:

  • Add a new "Web Application" to your VS 2008 solution (File->Add->New Project->C#->Web->ASP.NET Web Application).
  • Afterwards, you copy all the files in the old "website" to your newly created "web application", and override any files created in it by default
  • The next step is the most ugly, you need to "manually" add the references in your "website" to the new "web application". I thought the VS 2008 PowerCommands toy would do this for me as it does copy references from other project types, but it didn't. You have to do it by yourself, manually, and you have to be cautious in this step if you have multiple versions of the same assembly (like AJAXToolkit in my case) or assemblies that have both GAC and local versions or so.
  • Keep repeating the last step and trying to build the "web application". You'll keep getting errors like " '....' is unknown namespace. Are you missing an assembly reference? ". Make sure you have none of those except the ones where '....' is replaced by the IDs of the server controls you use. In other words, keep adding references and building the project until only the errors that exist because of missing .DESIGNER.CS or .DESIGNER.VB files.
  • Afterwards, go to the "web application" root project node in VS 2008 solution explorer, and right click it, then you WILL find the option "Convert to web application". What this option does is actually making small changes to the "@Page" and "@Control" directives of pages and controls, and creating the required .DESIGNER.CS or .DESIGNER.VB files.
  • Try building the "web application" again. If you get errors, see what references may be missing and/or go click the "Convert to web application" again. Sometimes, if there's any error other than those caused of missing DESIGNER files, not all the pages/controls will have those DESIGNER files created for them. Fixing the non DESIGNER problem and clicking "Convert to web application" again should do the job for this.
  • Once you are done successful VS build, you should be ready to go. Start testing your web application. Optionally, you can right click the "web application" root project node in VS 2008 Solution Explorer and click "Properties" then go to the tab "Web" to set the "web application" to a virtual folder in IIS (you can create new virtual directory from there in VS). If you want to use the IIS virtual directory that the old "website" used, you need to remove that from IIS first.
  • Update: When testing your pages, pay MOST ATTENTION to classes in "App_Code" folder, especially those with NO NAMESPACE. Those can be a big trap. We had a problem with two extension method overloads in the same static class that had no namespace,one extends DateTime? (Nullable<DateTime>) and calls another overload that extends DateTime itself. Calling the other overload as extension method passed VS 2008 compilation and gave us a compilation error ONLY IN RUNTIME (With IIS). Changing the call to the other overload from calling it as extension method to calling it as normal static method (only changing the call in the same class, calls from other classes remained extension method calls) did solve this one, but clearly, it's not as safe as it used to be in VS 2005. Especially with classes with no namespaces.
  • Update2: During the conversion, VS 2008 renames your "App_Code" to "Old_App_Code". This new name sounds ugly, but DO NOT RENAME IT BACK. In the "web application" model, all code will be in one assembly. In runtime, the web server does not know what web project type you are using. It does take all code in "App_Code" folder and create a new assembly for it. This way, if you have code in folder named "App_Code", you'll end up with RUNTIME compilation errors that the same types exist in two assemblies, the one created by VS, and the one created by IIS / ASP.NET Development Server. To avoid that. leave the "Old_App_Code" with the same name, or rename it to ANYTHING EXCEPT: "App_Code". Do not place any code in such "App_Code" folder and prefereably do NOT have a folder with such name in your "web application" at all.
    I know this since before but forgot it now as I have not used "website" model for long :(.

I hope this helps anyone to avoid my embarrassment, and still get rid of the weird errors of "website" model :).


Cross posted from weblogs.asp.net/meligy


Print | posted on Sunday, August 03, 2008 1:34 PM |

Feedback

Gravatar

# re: Converting VS 2008 Website to Web Application

I'm glad you pointed out the right-click convert to web application option. That saved me, and made this conversion a breeze.
11/18/2008 3:18 AM | Brandon Joyce
Gravatar

# re: Converting VS 2008 Website to Web Application

Bad! Bad! Bad! The conversion does not work as one would expect. The convert to web app is not smart enough to type the variables correctly. MS does it again.
1/23/2009 3:54 AM | JKP
Gravatar

# re: Converting VS 2008 Website to Web Application

Ah, the main point I gleaned from this was the fact that the only way to get the designer code behinds generated is to use that conversion option. I struggled for about 2 hours trying to figure out how to generate those files after-the-fact.

Thanks for the assistance.
2/4/2009 1:36 AM | NateDev
Gravatar

# re: Converting VS 2008 Website to Web Application

Really a very great post. Solves a lot of issues and clears out the concepts of doing a web project in VS environments.

Thanks,
Mudasser
http://www.cto247.com
http://www.cto247.com/blog
3/4/2009 3:45 PM | Mudasser Naeem
Gravatar

# re: Converting VS 2008 Website to Web Application

You'll keep getting errors like " '....' is unknown namespace. Are you missing an assembly reference? ".

I am getting these errors....how to rectify ? Iam new to development...pls tell me !!!!

And when I am getting these errors itself, I converted the into web application by right clicking root node :(

it shows 266 erroes now wherwas previously before converting, it displayed 144 errors :(

please help me immediately.... :(
3/26/2009 5:55 PM | Ram
Gravatar

# re: Converting VS 2008 Website to Web Application


I am getting these errors..You'll keep getting errors like " '....' is unknown namespace. Are you missing an assembly reference? ".
..how to rectify ? Iam new to development...pls tell me !!!!

And when I am getting these errors itself, I converted the into web application by right clicking root node :(

it shows 266 erroes now wherwas previously before converting, it displayed 144 errors :(

please help me immediately.... :(
3/26/2009 5:56 PM | Ram
Gravatar

# re: Converting VS 2008 Website to Web Application

Hey Raml
The solution is already in the post. It just sounds too stupid to be true :D

First you need to confirm that there are no "real" build errors in the project. Make sure all your real references are there and there is no compile time error except those you think are related conversion.
Then comes the solution:
Keep Converting!!!
YES. This is it. If you have no other "real" build errors, every time you do "Convert To Web Application" the number of errors is less and less until it reaches zero. This is not clear in small websites that usually need clicking ti one time only but for big projects you need to do the step many times until you get no errors. Stupid as it seems :D :D
3/26/2009 7:08 PM | Mohamed Meligy
Gravatar

# re: Converting VS 2008 Website to Web Application

Hello Sir,

My self Website developer, Our website was developed in VS2003, now I want to convert into VS2008, so how to convert all .aspx.vb pages and .ascx and .xsd files, I want to change aspx.vb to aspx.cs plz send me a procedure... step by step how to solve this problem...

Hope that you will send me needfull reply.
Thanking you sir,

Best Regards
inayath
3/30/2009 10:23 AM | Inayathullah
Gravatar

# re: Converting VS 2008 Website to Web Application

Great list. I followed the conversion steps as you described but when I ran the app I am getting a parser error: "An error occurred during the parsing of a resource required to service this request" where do I fix this?

Ahmed Zayan.
4/21/2009 11:45 PM | Zayan

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: