Home   Subscribe   Linkedin
  Archive Contact  

How to create custom configuration section in ASP.Net

In this article, I am going to describe how to create your own custom configuration section in Asp.net and how to use it, in a few very simple steps.

Step 1: Create a public class which you want to refer as the class that is holding up the config section, give it some name which shows its stores some kind of setting lets say “UserSettings” and make the class inherited by ConfigurationSection

public class UserSettings : ConfigurationSection

Step 2: Create some properties in this class which have the “ConfigurationProperty” Attribute in this manner

[ConfigurationProperty("MyVariable1", IsRequired = true)]
public string MyVariable1
{
    get { return (string)this["MyVariable1"]; }
    set { this["MyVariable1"] = value; }
}

Lets create three such test properties and then this is how the class looks like

public class UserSettings : ConfigurationSection {
    [ConfigurationProperty("MyVariable1", IsRequired = true)]
    public string MyVariable1
    {
        get { return (string)this["MyVariable1"]; }
        set { this["MyVariable1"] = value; }
    }

    [ConfigurationProperty("MyVariable2", IsRequired = true)]
    public string MyVariable2
    {
        get { return (string)this["MyVariable2"]; }
        set { this["MyVariable2"] = value; }
    }

    [ConfigurationProperty("MyVariable3", IsRequired = true)]
    public string MyVariable3
    {
        get { return (string)this["MyVariable3"]; }
        set { this["MyVariable3"] = value; }
    }
}

Step 3: Open the web.config file and define a Config Section under the tag <Configuration> like this,

<configSections> <section name="UserSettings"
     type="Example.Web.Utilities.UserSettings, Example.Web, Version=1.0.0.0, Culture=neutral" 
     allowLocation="true" allowDefinition="Everywhere" /> 
</configSections>

Now this is the information about the library that gets created when you “Build”

image_thumb9

next step, we tell the config section what “UserSettings” is,

<UserSettings
     MyVariable1="Value1"
     MyVariable2="Value2"
     MyVariable3="Value3" />

and altogether it looks like this

<configuration> 
     <configSections>
          <section name="UserSettings" type="Example.Web.Utilities.UserSettings, Example.Web, Version=1.0.0.0, Culture=neutral"  allowLocation="true" allowDefinition="Everywhere" />
     </configSections>
     <UserSettings 
           MyVariable1="Value1"
           MyVariable2="Value2"
           MyVariable3="Value3" /> 
     <system.web>
           <compilation debug="true" targetFramework="4.0" />
     </system.web>
</configuration>

Step 4: We go to the code behind of the default page of project and fetch the settings

public class Default : Page 
{
        protected void Page_Load(object sender, EventArgs e)
        {
            var userSettings = ConfigurationManager.GetSection("UserSettings") as UserSettings;
            if(userSettings == null) return;

            Response.Write(
                "MyVariable1 = " + userSettings.MyVariable1 + "</br>" +
                "MyVariable2 = " + userSettings.MyVariable2 + "</br>" +
                "MyVariable3 = " + userSettings.MyVariable3 + "</br>");
        }
}

Next, I go run the application and get this

image_thumb10

I have attached the sample project that we created through this post.

 
 

ConfigSectionExample.zip (6.58 kb)

Posted by: Zeeshan

Categories: ASP.NET, C#

Tags: , , ,

How to deploy Silverlight web application through installer

Suppose we want to create  MSI installer package of Silverlight web application for server deployment using Microsoft Installer.

For this, we need to utilize Microsoft Visual Studio "Web Deployment" project template and "Web Setup" project template.

Web Deployment project template comes only when we install its plugin. We can download its plugin from here.

Now in our web project we will right click and Add Web Deployment Project

1

 

This will prompt me for what I want to name the project and where to store its project file:

2

The web deployment project will then show up as part of my solution. We can change some optional settings by double clicking it

3

We have choose the default option for output assemblies  i.e  to merge the entire web-site into a single assembly that we can name whatever we want. This merges the output of all page/control compilation, app_code, and web-services into a single assembly (leaving one file to deploy).

Now, its time to add another project which is "Web Setup" project. This project actually creates MSI package. To add Web Setup project, right click on the solution in Solution Explorer and click "Add" then "New Project". Then, select "Web Setup Project" from Visual Studio project templates list. "Web Setup Project" will be available under "Setup and Deployment", which in Visual studio 2010 is under "Other Project Types".

4

We can see our project is added

5

Now, lets define the "Project Output" for the Web Setup project we just added. Right click on Web Setup project and click Add>Project Output

6

Now we will see "Project Output" dialog box. Project Output defines what output we want to include in our MSI package. In our case, we want output of pre-compiled website generated by Web Deployment project. So, select Web deployment project from "Project:" dropdown as your output project name and and select "Precompiled Web Outputs" from the listbox and click "OK".

7

This will add "Precompiled Web Outputs" to the Web Setup project.

8

Now, lets configure "Project Dependencies". "Project Dependencies" will set dependency or order in which each of the projects will get compiled. This will set the priority on how you want to perform your build.

9

Since our main Silverlight project is not dependent on anything, it should build first. Then our website should be build as it is dependent on Silverlight project. Then it should build WebDeployment project as it depends on WebSite build. Finally WebSetup project should build, which depends on both WebSite build and WebDeployment build.

image 

image

image

image

Noe we need to configure "Configuration Manager". "Configuration Manager" settings will tell Visual Studio which projects to include during "Release" build and "Debug" build.
To get "Configuration Manager" dialog, right click on the solution in "Solution Explorer" and click "Configuration Manager".

11

In "Configuration Manager" dialog, include all  projects for "Release" build and just include WebSite for "Debug" build. We normally create MSI package for server deployment only from "Release" build.

image

image

And Finally, we can build the entire solution by clicking "Build Solution

If everything goes fine and the build is successful, we will be able to see the MSI package under "Release" folder in Web Setup setup project file path

12

Now we can install this MyUserControl.msi

image

image

image

image

Now our web project has been deployed. We can check it by opening IIS

13

also we can browse our application

14

(Note Directory Browsing should be enabled )

image

and after clicking MYucTestPage we will get our result

image

I have attached the sample application..

MyucMsi.rar (459.43 kb)

Posted by: Mohd Ahmed

Categories: ASP.NET, IIS, msi, Silverlight

Tags: , , , ,