Wednesday, December 8, 2010

Converting a Web Site to a Web Application

To get our application to Windows Azure, we had to convert it from a Web Site to a Web Application.

I won't explain the differences between those two, you can go and Google it.

After some research, I found out the worst: there's no tool or wizard to do that, you have to do it manually. So open your Web Site solution, add a new Web Application project to it, and follow these steps.

First Step
Copy and paste everything. Copy everything on your Web Site, straight from the Solution Explorer, and paste it under the Web Application project. (don't forget to delete everything there first) To do that simply click the project and hit CTRL + V.

Second Step
Add the references again. You can check what are the references on the properties page for your Web Site. If it has any other projects references as well, you add them too. Keep adding them until you get no more missing reference errors.

Third Step
Now this is an amusing step. If you right click your newly created Web Application project, you'll notice an option that says: "Convert to Web Application". When you click that, Visual Studio basically reconnects your .aspx files with their corresponding code-behind files. All it does is change some minor things on the Page tags for your .aspx files. Possibly, you'll have to do this more than once, because if a page has a missing reference error, Visual Studio won't be able to work this on it, and it'll be ignored, so make sure you get all the reference errors first, and do this a couple of times.

Keep trying to compile until you get no errors. And pay attention to the App_Code. When you convert it to a Web Application, Visual Studio renames the App_Code folder to "Old_App_Code". Do not rename it back, leave it just the way it is. On a Web Application, your classes don't need to be inside an App_Folder folder to work, they can be anywhere. So if you want to rename it, rename to something OTHER than App_Code, because it may cause some multiple declaration problems.

Your code residing in the Old_App_Code may not be seen, although Visual Studio WILL NOT show you any compile-time errors. If it doesn't find your code, a runtime error will be thrown. To fix that, if you get any runtime errors, you just check where the calls are on your code, and put the cursor over it, Visual Studio will then probably show that it is not found, and suggest corrections. That happened to me, and in the end, all I had to do was reference the Web Application assembly (self referece, yes!), as it takes all classes in the project and puts it together in there. Be careful with namespaces here. And check for this everytime you get "Not Declared" or "Not Found" runtime errors.

So that's basically it, all I did was describe it all, in fewer and simpler words.

The post that guided me through all this can be found here.

Also, here's the official MSDN walkthrough.

No comments:

Post a Comment