How to convert ASP.NET website to ASP.NET web application

asp.netVisual StudioWeb Applications

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

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

Solution 2 - asp.net

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.

Solution 3 - asp.net

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.

Solution 4 - asp.net

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

Solution 5 - asp.net

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.

Solution 6 - asp.net

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!

Solution 7 - asp.net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRSolbergView Question on Stackoverflow
Solution 1 - asp.netAndrew HareView Answer on Stackoverflow
Solution 2 - asp.netKonstantin TarkusView Answer on Stackoverflow
Solution 3 - asp.netMatt BurrellView Answer on Stackoverflow
Solution 4 - asp.netMarcoView Answer on Stackoverflow
Solution 5 - asp.netTim SamandariView Answer on Stackoverflow
Solution 6 - asp.netSunny R GuptaView Answer on Stackoverflow
Solution 7 - asp.netMikeView Answer on Stackoverflow