96

I have an ASP.NET 3.5 Website (visual studio lingo), but the site continues to grow and is looking rather cowboyish among other things. I'd like to see this get converted into a Web Application (namespaces and all).

Is this something that can be easily done in Visual Studio? If not, are there any other tools out there that could create all of the namespaces, etc. automagically?

4
  • 4
    That website template is a really awful POS compared to the Web Application template. I fail to see any benefit of using the website template...
    – James
    Commented Apr 9, 2009 at 17:08
  • 10
    @James - just one of many reasons I'm looking to move away from it... You are preaching to the choir....
    – RSolberg
    Commented Apr 9, 2009 at 17:10
  • 2
    I am sure the walkthrough below is very thorough (didn't read all of it), but a person on youtube showed a very simple way to convert a website to a web app here: youtube.com/watch?v=oXptokM0v7w - as usual I can't guarantee that this will work for everyone. I think this method will work for people with smaller less complicated sites. Commented Apr 5, 2012 at 16:23
  • 3
    If you are using VS2013, don't forget the menu item has moved.
    – Jon
    Commented Nov 11, 2014 at 20:38

7 Answers 7

113

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" !!!!

[emphasis mine]

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

10
  • 2
    Your link is broken at the top, Error 500 Commented Dec 25, 2011 at 16:59
  • 6
    Also, look for code files to get copied in as "content" and not "compile"
    – jcolebrand
    Commented May 22, 2012 at 21:54
  • 18
    For those of you doing this on VS2013, "Covert to Web App" has been moved to the Project menu, at the bottom. Commented Apr 20, 2014 at 6:35
  • 3
    @EricSassaman But it's not there at all for Web Site projects for me.
    – NickG
    Commented Jun 13, 2014 at 10:34
  • 1
    @NickG, you are correct, as per the first line of the answer, it is NOT there for website projects, the Project menu doesn't even exist unless you select a Web Application in the Solution Explorer. If you see "Website" on the menu, it's not a Web Application. Follow the steps above to create a blank Web Application, copy your files into it. Then "Convert to Web Application" should be on the project menu for your new Web Application. Did you do that? Commented Jun 20, 2014 at 18:11
16

Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio at MSDN

If your website application grows.. it's better to split it into several projects. Conversion from Web Site project to Web Application project won't help much.

7

If you're having problems getting your new Web Application Project to build check the File Properties in Visual Studio of all 'helper' classes. For a project I was converting the Build Action was set to Content whereas it should have been Compile.

5

I've now successfully migrated one Website project to a web application and there is quiet a few gotchas to look out for.

Having ReSharper at your disposal helps a lot in refactoring the aspx files.

  1. Set up your solution and create an empty WebApplication
  2. Copy all file over
  3. aspx files in website projects don't have a namspace. Wrap your classes in the appropriate namespaces
  4. During copying, all my pages in subfolders got renamed to my project name and the foldername, so I got 40ish public partial class FolderName_Projectname : Page If neccessary rename all files using Resharper or manually. If you encounter multiple errors like "There is already a member Page_Load() defined", this is most likely due to incorrect class names und duplication
  5. After adding a namespace
  6. Replace CodeFile in all aspx pages with Codebehind and especially pay attention to files i your subfolder. Make sure Inhertis="" doesn't contain the relative path. Your namespaces take care of everything. So the correct format is Inherits="Namespace.classname". If your class has a namespace NaSpa and a filename foo.cs it would be Inherits="NaSpa.foo"

  7. After you have prepared all your files (don't forget your master pages), run "Convert to web application". If you encounter errors afterwards, rinse and repeat. If you encounter errors of the sort "TextBoxName can't be found are you missing a reference", make sure you did not forget to sanitize your aspx pages. A good indicator is to check the automatically generated designer files. If TextBoxName does not appear in there, the conversion did not succeed completely.

  8. Resolve any missing dependencies.
  9. Build
4

Create a New Web Application in VS 2010.
1. Using Windows Explorer copy all your files into you project folder.
2. In VS 2010 solution explorer show all files.
3. Select the files and folders - right click include in project.
4. Right click the project solution explorer and select Convert to Web Application.

There are quite a few small differences, such as the App_Code folder will get renamed to old_app_code - that surprisingly doesn't cause any errors. The TypeName on your object data sources and the inherits on the @Page tag might need the [ProjectName]. prefix appended globally. For example if your type name was "BusinessLogic.OrderManager" and your project name is InventorySystem you would need to change it to InventorySystem.BusinessLogic.OrderManager. Also a few display changes, such as required field validators don't default to red font anymore, they default to black.

1
  • my app code file did not change like it was suppose to. I had to mannually rename it then change all the cs pages to compile.
    – Mike
    Commented May 3, 2016 at 17:38
4

I was facing the same problems initially. After following the Wrox Professional ASP.NET 4.0 book, I found the following solution for my case.

I first created a new web application. Copied all the website files into the web application folder. Right click on the application, and click conver to web application.

You might ask why you need to convert a web app into a web app. The answer is, that when you create a website, you simply code the .cs file where-ever required. A web application, however declares .design.cs (or .vb) and a .cs file for the code and design section automatically.

NEXT: Remove all manual references, like 'Inherits' attribute in the PAGE directive, to other files in your website, since name spaces WILL take care of referencing the classes centrally.

I also faced a problem, since I had not included OBJ and BIN folder in my project. If you think you are missing your BIN and OBJ folders, simply click the 'Show All Files' icon in the Solution Explorer and then right click on the missing folders and add to project. (to make sure they compile with the project.)

UPDATE: As @deadlychambers points out in the comments: You can search everywhere by doing a "Ctrl + Shift + F" and then search for Inherits="(.*?)". This will find all occurrences and probably save you some time!

3
  • 1
    You can remove all with Ctrl-shift-F and then change the search options for regular expression and use this Inherits="(.*)?" Commented Nov 17, 2014 at 19:40
  • 1
    Check that is is Inherits="(.*?)" the other one will grab everything behind inherits until the last quotation mark. Commented Dec 13, 2014 at 20:24
  • 1
    I just ran into an issue where NOT having inherits caused the designer to delete the namespace then delete the class. I wish this was easier. Commented Dec 13, 2014 at 21:07
0

the default ASP name space does not seem to work anymore. So I cannot seem to call my User Controls.ascx pages from outside the page. Giving them a namespace and changing the default from ASP to my namespace seemed to work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.