Blog Moved To http://weblogs.asp.net/meligy

Cross posting from the new blog @ http://weblogs.asp.net/meligy

  Home  |   Contact  |   Syndication    |   Login
  308 Posts | 3 Stories | 146 Comments | 110 Trackbacks

News

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

SponsoredTweets referral badge

Twitter












Tag Cloud


Article Categories

Archives

Post Categories

BlogRoll

About Me:

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


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

Feedback

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

# re: Converting VS 2008 Website to Web Application 1/23/2009 3:54 AM JKP
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.

# re: Converting VS 2008 Website to Web Application 2/4/2009 1:36 AM NateDev
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.

# re: Converting VS 2008 Website to Web Application 3/4/2009 3:45 PM Mudasser Naeem
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

# re: Converting VS 2008 Website to Web Application 3/26/2009 5:55 PM Ram
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.... :(

# re: Converting VS 2008 Website to Web Application 3/26/2009 5:56 PM Ram

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

# re: Converting VS 2008 Website to Web Application 3/26/2009 7:08 PM Mohamed Meligy
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


# re: Converting VS 2008 Website to Web Application 3/30/2009 10:23 AM 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 4/21/2009 11:45 PM Zayan
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.

# re: Converting VS 2008 Website to Web Application 8/11/2009 12:05 PM Mohamed Meligy
@Ahmed Zayan
Try deleting the .resx file associated with the page you having trouble with.

# re: Converting VS 2008 Website to Web Application 8/26/2009 6:27 PM Walter Gray
I followed the steps and have no reference errors. But every file says
Type pagename already defines a member called 'Page_Load' with the same parameter types .

What's up with that?

# re: Converting VS 2008 Website to Web Application 8/26/2009 6:42 PM Mohamed Meligy
Can you tell me the exact error message as it appears in the errors window?

Also, try to delete all .designer.cs or .designer.vb files from your application.

Also, do all the pages in your site inherit a custom base class or inherit the System.Web.UI.Page class directly?
If custom class, is it in the same site or not, and, is it in the "App_Code" folder or not?

Did you add all the DLL and Project references you may need before conversion?

# re: Converting VS 2008 Website to Web Application 8/26/2009 6:58 PM Walter Gray
Yes I think it is also a namespace issue. I did not have any of the pages in a namespace. Is there a fast way to reconfigure the namespace on every file or is it one by one?

# re: Converting VS 2008 Website to Web Application 8/26/2009 9:51 PM Mohamed Meligy
hmm .. IF you are using Resharper plugin, you can right click the project node in Visual Studio and click Cleanup Code" and from the next dialog choose "Full Cleanup".

If not, you can use search and replace.. something like:
CTRL + H (Find and Replace) =>
Look In: Current Project
Expand "Find Options" -> Select "Use:" "Regular Expressions"

Find What:
public partial class :i \: System.Web.UI.Page((.)*\n(.))*
Replace With:
namespace MyNamespace\n{\n\0\n}

This will include the namespace MyNamespace in all page classes. You'll still need to do another change in the aspx files themselves.
Same find and replace as before except...

Find What:
Inherits="
Replace With:
\0MyNamespace\.

I hope this works for you. Please tell me how it goes.

# re: Converting VS 2008 Website to Vs 2005 10/6/2009 6:05 PM Mohit
plz tell me how to convert 2008 project to 2005

# convert asp.net web application to windows azuretion 11/24/2009 4:40 PM sumanth
please how to convert asp.net web application to windows azure

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: