Converting a Web Site Project to a Web Application Project

Converting a Web Site Project to a Web Application Project

Rate This
  • Comments 6

Differences between Web Site Projects (WSP) and Web Application Projects (WAP) are highlighted in blogs such as this one. Based on that, if you feel a WAP would be better for your particular needs than a WSP, but have already created a WSP, you may be asking yourself, “Can I convert my WSP into a WAP, without starting from scratch?”.

This posting explains how to convert an existing Web Site Project to a Web Application Project in Visual Studio 2010. The most striking differences to a Web Site Project are that WAPs have a project file to include and exclude files, and compile to a single assembly.

The guidelines below include several of the basic steps detailed in the Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio. This is an excellent topic to review as it discusses some specific issues you may encounter which were discovered in previous versions of Visual Studio (VS). Please keep in mind while reading it that it is based on a WSP to WAP conversion using previous versions of Visual Studio.

Let’s get started.

Open and Verify your Visual Studio Web Site Project
Before converting your WSP to a WAP, you should open it in Visual Studio and verify that is it working correctly.  This will help prevent the need to research errors that have nothing to do with the conversion process.

  1. In the File menu, click Open Web Site.
  2. The Open Web Site dialog box is displayed.
  3. Select the project folder that you want to open, and then click Open.
  4. In the Build menu, click Build Web Site.
  5. In the Debug menu, click Start Debugging. Alternatively, you can press F5.
  6. Verify your project compiles and runs as expected

Create a new, empty Visual Studio WAP
A good strategy for converting a WSP to a WAP is to create a new, blank Visual Studio Web Application Project in a separate directory, but in the same solution. This avoids changing any part of the existing Web site files. It also allows you to copy existing functionality and files into the new WAP easily, within the same Visual Studio instance.

  1. In the File menu, click Add, and then click New Project.
  2. The Add New Project dialog box is displayed.
  3. In the Installed Templates section of the Add New Project dialog box, expand the language that you want to use, and then select Web to display the Web-related templates.
  4. Select Empty ASP.NET Web Application.
  5. Type values for Name, Location, and then click OK to create the Web Application Project.
  6. After the project has been created, delete the Web.config file that is created automatically.

WSPandWAP

Set Project / Assembly References
If the WSP required additional project or assembly references, you need to add them to the WAP. You can see the list of default references associated with the new (empty) Visual Studio Web Application Project under the References node in Solution Explorer.

  1. In the Solution Explorer, make sure Show All Files is turned on.
  2. In the Solution Explorer, right-click References, and then click Add Reference.
  3. The Add Reference dialog box is displayed.
  4. Select the reference that you have already added in the Web Site Project and then click OK.
  5. Note: To help prevent errors, add references to the Web Application Project for assemblies that existed in the \bin folder of the WSP.

AddRef 

Copy and Convert the App_Code folder from the Web Site Project to the Web Application Project
In WSPs, the files in the App_Code folder are all compiled together and then referenced (automatically) as a “dll” by all other files in the WSP. In WAPs, this is not the case. All code is compiled together as one .dll. I’ve found that copying the App_Code folder over first and converting it to the WAP model helps to head off some dependency issues which could arise if one copied the entire site, converted, and then tried to compile.

  1. In the Solution Explorer, copy the entire App_Code folder from the WSP to the WAP
  2. In the Solution Explorer, select the WAP’s root node; right-click, select Convert to Web Application
  3. You will see our standard Convert to Web Application confirmation dialog.  Select “Yes” to this dialog.
  4. The App_Code folder should now be renamed to Old_App_Code folder
  5. Note: Do NOT name it back. As mentioned above, in the WAP model all code will be compiled into one assembly. At runtime, ASP.NET doesn’t know what type of project model you have created and will take anything in the “App_Code” folder and create a dynamic assembly for it, thereby causing “could not load type” exceptions as there would be duplicate types exists in two assemblies (the one for the VS web application and the one for App_Code).  Learn more about why App_Code folder does not work well with WAPs.
  6. Compile the WAP
  7. If you see an error, the most likely causes are:
    1. Missing assembly reference. See the section above to add missing references to your project.
    2. Files marked with Build Action = Content instead of Build Action = Compile. Any file you want compiled should be marked as such.
    3. To set the Build Action property:
      1. Select the file in the Solution Explorer
      2. Press F4 (Brings up the File’s Property Grid)
      3. Look at the Build Action property. Change as necessary.

converttowap

Copy and Convert the remaining Files and Folders from the WSP to the WAP
Once your Old_App_Code folder is compiled (by compiling your WAP in the step above), your WAP will have an assembly in the \bin directory. This will make it easier for the remaining files you are copying over to successfully compile, especially if they reference or use code in the files contained within that folder.

  1. Select the remaining files / folders from the WSP project and copy them into the WAP.
  2. Make sure not to copy the App_Code folder again.
  3. In the Solution Explorer, right click the root node of the WAP and select Convert to Web Application.
    1. Don’t worry, this won’t affect (or try to reconvert) any files or the Old_App_Code folder you have already converted.
  4. Note: This will cause VS to automatically generate a .designer.cs (or .vb) file for each page, user-control, and master page in the project. Additionally, each .aspx/.ascx will be modified to use the ‘Codebehind’ instead of the ‘CodeFile’ attribute in the Page directive.
    1. Example of the ‘CodeFile’ to ‘Codebehind’ change
      1. WSP file:  <%@ Page Title=”Home Page” Language=”C#” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
      2. Converted WAP file:  <%@ Page Title=”Home Page” Language=”C#” Inherits=”_Default” Codebehind=”Default.aspx.cs” %>

convertedfiles

Compile your WAP
After all the files have been added and converted, you should build your project again to see if there are any compilation errors. At this point, some of the most likely causes of errors are:

  1. Code files set with Build Action = Content. These should be changed to Compile. Refer to the above section.
  2. Missing project or assembly references. See above for steps for adding project or assembly references.
  3. Class name collisions. In the WSP model, each .aspx and associated codefile was compiled together as a separate, individual unit. Therefore, one could have files such as foo\widget.ascx.cs and bar\widget.ascx.cs where the classes were the same. Once these files are moved to a WAP and all compiled together, class name collisions occur with errors something like, “has multiple definitions with identical signatures”. If this occurs, unique class names should be created.

Run your WAP
After completing the above steps and you have a WAP which successfully compiles, you are ready to try running your application. One of the most common problems I’ve seen encountered is the “Unknown server tag ‘SomeTag: Control’ (as it applies to user controls and such)”. This can be corrected in one of two ways.

  1. Add or modify the register directive on the page that is using the control. This will only register it for this specific page.
    1. <%@ Register Namespace”namespace the control is in” TagPrefix=”SomeTag” Assembly=”Name of dll compiled into the \bin folder” %>
  2. Register the control in the Web.config, making it available to all pages in the project
    1. In the <pages><controls> section add the following:
    2. <add tagPrefix=”SomeTag” namespace=”namespace the control is in” assembly=” Name of dll compiled into the \bin folder” />

Hope this helps!


Nichole Baker
SDET, Visual Studio Web Tools

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
Page 1 of 1 (6 items)