About My Blog



Whenever I get stuck doing something - the time comes to venture in the world of internet to find solution. In most cases I do find the solution, solve my problem and go on with my life. Then one day I am faced with the same problem. But now - I can't remember how the hell I solved it the first time. So the cycle begins again. Then I thought, what if I can remember all the those things? So here it is, my Auxiliary Memory. I decided to save all the problems and their solution in this blog so that I can get back to them when I need them. And the plus point is - so can everybody else.

Thursday, June 2, 2016

Converting Visual Studio Website to Web Application

Recently I had to convert an old visual studio website project to web application project because I needed to generate a class diagram of the project and visual studio 2013 didn’t let me generate one for website project. I google around and read article on how to do it. In most places it says to create a new web application project and then copy the files in the new project. Then right clicking the web application project will give you the option to “convert to web application”.

I followed the instruction and tried to do that. However, for some reason when I right clicked the new project I didn’t get any convert to web application item. Instead I got “convert to class library”.

So I thought I will manually make the conversions required. So I created a new web application project and added a webform to the project to see how it differs from the ones I already had from the web site project.

There were mainly three differences -

  1. The class in code behind file was under namespace.
  2. Each aspx page also had a associated designer.cs page with it.
  3. The header of the aspx pages had some different information.

Let’s take a sample website project structure which I will convert to web application. The structure of the project is as follows -

Website
    - App_Code
        - BLL
            - GeneralBLL.cs
            - AccountBLL.cs
        - DAL
            - GeneralDAL.cs
            - AccountDAL.cs
    - Masterpage.master
    - Masterpage.master.cs
    - Default.aspx
    - Default.aspx.cs
    - Account
        - Login.aspx
        - Login.aspx.cs
    - Web.config

Now I will copy these files to my new web application project so that the structure looks like -

Webapplication
    - BLL
        - GeneralBLL.cs
        - AccountBLL.cs
    - DAL
        - GeneralDAL.cs
        - AccountDAL.cs
    - Masterpage.master
    - Masterpage.master.cs
    - Default.aspx
    - Default.aspx.cs
    - Account
        - Login.aspx
        - Login.aspx.cs
    - Web.config

One noticeable changes to the structure is that there is no more App_Code folder. Everything inside the folder was shifted to root or home folder.

One thing to note that it’s better not to overwrite the default Web.config file of the web application project. Instead copy the required segments from old config file to the new one.

Now, let’s start making all the changes to run the new project.

Adding namespaces to classes

As far my understanding goes, all classes in website application are place in a single (global) namespace (I might be wrong). But, web application uses namespaces to organize the classes.

Generally, any classes placed in the root directory have project name as their namespace. Classes that are under folders get the folder names appended with the project name to form their namespaces.

//Default.aspx.cs
namespace Webapplication
{
    public partial class _Default
    {
    //.....
    }
}

//Login.aspx.cs
namespace Webapplication.Account
{
    public partial class Login
    {
    //.....
    }
}

For other cs files (like GeneralBLL.cs, AccountDAL.cs), I created namespace according to their folder structure. So GeneralBLL class had namespace Webapplication.BLL, AccountDAL class had namespace Webapplication.DAL, and so on.

Adding designer class file

In web application project each webform page has an associated designer.cs file. This file is not needed for website projects. This file is used by VS to declare all the control variables as class variables. So, I have to add this file for each webform file. The name of the physical file have to match correctly (e.g. for Default.aspx - Default.aspx.designer.cs). Class name in both files have to match and both should be declared as partial. Also, namespace of both files should match.

Making changes ASPX headers

In an website project the header of Default.aspx page looks something like this -

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

In an web applicaiton project the same file looks something like -

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Webapplication._Default" %>

Both are almost same, except for two changes. One is that the CodeFile parameter is changed with CodeBehind. And second is that the Inherited class is now under a namespace (In our case it’s Webapplication).

Interestingly when the changes in header is saved, visual studio automatically changes the designer file and add declarations for all the controls defined in Default.aspx file. This is a real time saver when there are a lot of controls in a page. Also this provides a indication that all the changes we made upto now actually works.

For aspx files under folders, website project names the class file with the foldername as prefix. For example, the class name for Login.aspx.cs file would be Account_Login since the file is under Account folder. For folders I created separate namespace like Webapplication.Account and rename the class as simply Login. This also has to be set accordingly in the aspx file header.

For masterpages same process have to be followed.

After the changes, the structure of Webapplication became

Webapplication
    - BLL
        - GeneralBLL.cs
        - AccountBLL.cs
    - DAL
        - GeneralDAL.cs
        - AccountDAL.cs
    - Masterpage.master
    - Masterpage.master.cs
    - Masterpage.master.designer.cs
    - Default.aspx
    - Default.aspx.cs
    - Default.aspx.designer.cs
    - Account
        - Login.aspx
        - Login.aspx.cs
        - Login.aspx.designer.cs
    - Web.config

For the project I was working on, I had to change all the files in the project this way. It is indeed a hectic job, and sometimes I missed one or two files. However, building the application always produced errors so I could check for the missing files easily. But once I did all that changes, the project ran smoothly.