Sunday, August 03, 2008 5:37 PM Mohamed Meligy

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 :).

Filed under: , , , , ,

Comments

# re: Converting VS 2008 Website to Web Application

Sunday, August 03, 2008 9:58 AM by Mohammed Nour El-Din

Nice Hint :)

# Convert Your VS 2008 Website to a Web Application

Monday, August 04, 2008 6:49 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Converting VS 2008 Website to Web Application

Wednesday, September 17, 2008 10:16 AM by silent

Now I'm converting a VS 2003 web application into VS 2008, I think I don't need to perform "Convert to web application", right?

I tried to do "Convert to web application", then many additional *.designer.cs were generated. I'm not sure what this convertion for ? Should i do it or not?

Please advice, thanks a lot.

# re: Converting VS 2008 Website to Web Application

Wednesday, September 17, 2008 10:23 AM by Mohamed Meligy

When you migrate VS 2003 web project to VS 2005 with SP1 installed or VS 2008, the migration used the web application model not the website model by default.

The additional .designer.cs files are just normal. This is the replacement in VS 2005 and later to the "Designer Generated Code" C# region that VS 2003 places in .cs files for the use of VS designer.

You should be OK by just opening the .csproj or .sln file inside VS 2008.

# re: Converting VS 2008 Website to Web Application

Thursday, October 09, 2008 6:06 AM by Wael Muhammad

Thanks, a very useful article, I faced most of the mentioned points and this article helped me a lot.

# re: Converting VS 2008 Website to Web Application

Sunday, November 02, 2008 2:47 PM by benhart

I've just faced the same problem! I'm too a little red faced after repeatedly saying "just right click the site, and select "Convert to Web Application"! Doh, that item no longer exists. What a joke!

Thanks for your steps, I appreciate having them as a guide. I'm still a little bitter that I have to follow them in the first place, though :)

# Repeat after me, "check yourself before giving bad advice"

Monday, November 03, 2008 2:00 PM by Ben Hart

Repeat after me, "check yourself before giving bad advice"

# re: Converting VS 2008 Website to Web Application

Friday, January 23, 2009 2:26 AM by jkp

!!!! BEWARE !!! - This does not work for web sites that have nested user controls.  The convert to web application is not recursive and cannot create the designer files appropriately.  It only appears to go one level deep when converting user controls which causes errors.

# re: Converting VS 2008 Website to Web Application

Tuesday, January 27, 2009 6:57 AM by Rostyslav

Thanks

# re: Converting VS 2008 Website to Web Application

Wednesday, February 18, 2009 5:45 PM by MaxAwesome

When converting to a Web Application, after the conversion process has created your designer.cs file, be sure that the class definition within that file is correct.

In my case, as an example, I had one aspx file, Default.aspx, with Default.aspx.cs associated to it. The class definition within Default.aspx.cs was:

public partial class _Default : System.Web.UI.Page {...}

Default.aspx was inheriting the "_Default" class. Fine, great. That's expected. That's how it was in my Web Site project and it worked fine.

After some build failure frustration, I discovered that Default.aspx.designer.cs was auto-generated using the following class definition:

public partial class Default {...}

All the controls were there. They were all named correctly. The problem was that Visual Studio, for whatever reason, did not want to use "_Default", but instead, "Default" in the designer class file's class definition.

Hopefully that will help someone avoid the frustration I experienced from this small oversight.

# re: Converting VS 2008 Website to Web Application

Monday, March 30, 2009 4:32 AM by Inayathullah

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

# re: Converting VS 2008 Website to Web Application

Thursday, April 16, 2009 6:42 PM by Michael Schuld

Thanks for that. I had a TON of "errors" in ReSharper that have now disappeared as it knows how to find all of my controls.

# Unit test | hilpers

Saturday, April 18, 2009 12:05 PM by Unit test | hilpers

Pingback from  Unit test | hilpers

# Web site VS Web application in .Net

Monday, May 04, 2009 4:20 AM by Web site VS Web application in .Net

Pingback from  Web site VS Web application in .Net

# Repeat after me, &quot;check yourself before giving bad advice&quot; | UK Web Designer

Pingback from  Repeat after me, &quot;check yourself before giving bad advice&quot; | UK Web Designer

# ASP.NET MVC Grid ??? Part 4 &#8211; View Models, Unit Testing, Keyword Search &laquo; RAD for N-Tier web apps in .NET

Pingback from  ASP.NET MVC Grid ??? Part 4 &#8211; View Models, Unit Testing, Keyword Search &laquo; RAD for N-Tier web apps in .NET

# re: Converting VS 2008 Website to Web Application

Friday, July 24, 2009 8:41 AM by Lee

I have converted my old VS2008 Website to Web Application, now everything was working before I tried to convert it. But now I don't seem to be able to reference my Classes? For example I have a BasePage class that every .aspx page inherits like so

public partial class SomePageName : BasePage

{

}

But now I get this message? And the same for all the other classes?

The type or namespace name 'BasePage' could not be found (are you missing a using directive or an assembly reference?)

How do I find out which 'using' directive I am missing and whats an assembly reference?

# re: Converting VS 2008 Website to Web Application

Friday, July 24, 2009 9:19 AM by Mohamed Meligy

Where is the PageBase class defined? In the website/application or in a referenced library?

If it was inside the website app_code folder before conversion, where is it located in the new web application?

Also, is the PageBase class included in a namespace? IF not try to put it in a namespace.

# re: Converting VS 2008 Website to Web Application

Thursday, August 06, 2009 5:05 AM by ankarao

i am trying to Convert VS2008 web site project to Web Application Project. i gone through documentation of this topic.

Created a new web application project and copied all files and folders from web site project to newly created Web Application Project.  we need to add References manually.  i right click project node ---> Add references ----> Browse   here i can browse only .dll, .exe, manifest and some more files.  but in our project we have .aspx files and some other files.

so i am not able to add references for .aspx files and etc.

# re: Converting VS 2008 Website to Web Application

Thursday, August 06, 2009 5:46 AM by Mohamed Meligy

You do not need to "Add reference" but to include the files.

In Solution Explorer tool window, there are some icons just under the window title. Look for the icon to the left to the "Refresh" icon, it reads "Show All Files" when you hover it.

Click this icon and you'll see the files you pasted. Select those files, right click them, and from the right click menu choose "Include in project", then go to the web application Project node again in Solution Explorer, right click, and choose "Convert to Web Application" as the above steps say.

If you have trouble with showing those files, it may be easier to delete the files you copied, copy the original files again, but do NOT paste the file in the web application folder in Windows Explorer, instead do this: go to Visual Studio, right click the Web Application Project node in Solution Explorer, you'll find in the right click menu an option "Paste". Use this to paste your files, and continue with the steps normally.

# re: Converting VS 2008 Website to Web Application

Saturday, August 08, 2009 1:56 AM by espresso

I've done all these steps and still, none of my .aspx can reference my user or custom controls.  And none of my .ascx can reference my custom controls.  I've tried everything.

# re: Converting VS 2008 Website to Web Application

Saturday, August 08, 2009 6:45 AM by Mohamed Meligy

From what you are describing, it seems like  "Convert to Web Application" step is not having effect for you. This can happen if you have build errors.

Please open the original website in its original VS Solution and confirm that the entire solution compiles correctly, then, go to the web application (maybe better create yet another new Web Application), go add reference to ALL the libraries that original website had reference to (or were in its 'bin' folder). Make sure all references are there.

Now, cop the files from original website and paste them in the new Web Application (using VS for Paste, not Windows Explorer).

Then try "Convert to Web Application" on the newly created web application two or three times and build the web application after every time. See if the build errors are less after every time. Also, search in the build errors for any other errors than those generated because the pages and controls are not seeing each other.

Check if there are ".aspx.designer.cs", ".ascx.designer.cs" OR ".aspx.designer.vb", ".ascx.designer.vb" were generated for your web application after "Convert..." or no.

If you still have problems, send me a screenshot of your new Web Application in VS if possible, or at least, copy the complete list of errors and warnings you are getting on build.

# re: Converting VS 2008 Website to Web Application

Friday, September 25, 2009 2:45 PM by mhat

Excellent Article

For those using a custom ASP.NET Profile class, they may have an Update3 as well. To resolve any Profile build errors when converting from a WebSite to a Web Application, they may reference the following article:

weblogs.asp.net/.../writing-a-custom-asp-net-profile-class.aspx

# re: Converting VS 2008 Website to Web Application

Friday, November 06, 2009 4:37 PM by larryv viezel

So I followed these instructions. Every time I open the application after that it immediately converts it to a website and I need to start the process all over again. any suggestions?

# re: Converting VS 2008 Website to Web Application

Friday, November 06, 2009 4:48 PM by Mohamed Meligy

You cannot easily convert a web application back to website as per my understanding.

Are you sure you are openning the new project you created not the old website?

Leave a Comment

(required) 
(required) 
(optional)
(required)