URL Remapping in .Net 2.0

23 01 2009

In this article I will walk through a concept for URL Remapping in .Net 2.0

The need for url remapping can arise due to several reasons like
Generating SEO friendly urls
Hiding the actual page or other data from web users.
Control over each and every entry and exits for web requests.
.Net 2.0 gives flexibility to define and remap urls in web.config file as well as generating your own Custom URL Remapper HTTP Module.
I will explain the first approach i.e remap urls in web.config in this post and will try to wrap up the second one in my next article with a pretty decent code sample for building Custom URL Remapper HTTP Module.
Remapping URL’s in web.config file.
To Map urls .Net provides a section named  urlMappings in web.config file where one can define the collection for their mapped urls.

  <add url=”~/Home.aspx” mappedUrl=”~/Default.aspx”/>

Here whenever the request for Home.aspx will come the .Net engine will search for  urlMappings section in web.config file and server the page that is mapped with it. In the above mention example it will server Default.aspx page.
Limitations: To have more control over the urls, regular expression cannot be used. So it is better to build our own  URL Remapper HTTP Module.
Live World Scenario:
In case of shopping cart with thousands of product categories and catalogs it is very important to make this page SEO Friendly. Though SEO includes several criteria depending on different robots basic idea of URL remains the same. So by using URL Remapping we can generate SEO friendly URLS that robots can easily search.
Like say for example we are having three products categories viz
We can map all the three categories to a single page that servers the purpose of displaying these products based on the product name
So we map all the three to Product.aspx page like shown in the below section.

  <add url="~/CellPhones.aspx" mappedUrl="~/Product.aspx"/>
  <add url="~/Computers.aspx" mappedUrl="~/Product.aspx"/>
  <add url="~/DigitalCameras.aspx" mappedUrl="~/Product.aspx"/>

Now whenever the request for  CellPhones.aspx,  Computers.aspx or DigitalCameras.aspx comes .Net engine will compile Product.aspx page and will server to the requesting client. In Product.aspx page we can extract the name and make calls to database to get all the products related to the given category.


To Download code sample Click Here


Modify Web Configuration file without restarting asp net worker process (aspnet_wp).

19 10 2008

Modify Web Configuration file without restarting asp net worker process (aspnet_wp).

No one in the world can say that web.config file will remain intact after the deployment. Modifications have to happen to some or the other parameters of the web configuration file like one has to add or remove key value pairs from appsettings section etc…

So what?
As soon as the webs configuration file is saved the aspnet_wp worker process of that particular web application gets restarted and all the current sessions and data are lost.

This can cause tremendous issues to an organization whose application needs to be up for 24*7.

To recover from such kind of scenario Microsoft has already provided a great tweak to handle this.

Following are some steps that need to be followed.
Step 1) Make a separate configuration file lets say for example appsettings.config with the following xml data.

<?xml version="1.0"?>
  <add key="Name" value="Dhaval"/>
  <add key="Surname" value="Upadhyaya"/>

Step 2) Now in the original web.config file make your appSettings section look like below

<appSettings configSource="appsettings.config">

You are done.

Place a debug point on the Application_Start event in global.asax file and check that changing appsettings.config does not restart the worker process.

How to Encrypt or Decrypt sections of Configuration file.

15 08 2008

How to Encrypt or Decrypt sections of Configuration file.

It is always a good practice to protect sensitive information in configuration files. One should always protect connectionStrings section from being easily readable; also some times it becomes necessary to encrypt appSettings section that may include some sensitive data. You know why…?

So in this Blog I will walkthrough the steps required for encryption\decryption of sections in configuration file.

.Net framework ships two providers for this purpose.
RsaProtectedConfigurationProvider and DpapiProtectedConfigurationProvider.
Here I will use RsaProtectedConfigurationProvider.

Step 1) First off all we will add provider to configuration section of web.config file that you want to play with. This provider is required for actual processing done while running commands depicted in step 3 and 4.

<configProtectedData defaultProvider="myProvider">
  <add name="myProvider"

 RsaProtectedConfigurationProvider requires key container for encryption and decryption purpose. This container includes private\public keys that are required during encryption and decryption process by the attached provider.

NetFrameworkConfigurationKey : This is the default key container shipped by Microsoft. You can also create your own key container that includes private\public keys required for encryption\decryption. In order to avoid confusion I will depict these steps in my next article.

Note: As we are using default key container “NetFrameworkConfigurationKey” you can do away with the first step. This step will be added by machine.config file cause it includes declaration for both providers  (“RsaProtectedConfigurationProvider” and “DpapiProtectedConfigurationProvider”). But this will be helpful incase we are creating our own key container.

Step 2) After adding the provider in Step 1 go to the Visual Studio command prompt. Now In order to access “NetFrameworkConfigurationKey” key container your “ASPNET” account must have permission to access it. Run the below command to give access.

aspnet_regiis -pa “NetFrameworkConfigurationKey” “ASPNET”


Step 3) Run below command to encrypt connectionStrings section of your web.config file located in the virtual directory named “MyWebApplication”.

aspnet_regiis -pe connectionStrings -app /MyWebApplication


Step 4) Run below command to decrypt connectionStrings section of your web.config file located in the virtual directory named “MyWebApplication”.

aspnet_regiis -pd connectionStrings -app /MyWebApplication

Note: You can replace the “connectionStrings” section with the section name you want to encrypt\decrypt.
Eg: aspnet_regiis -pe appSettings -app /MyWebApplication
This will encrypt “appSettings” section of your web.config file located in the virtual directory named “MyWebApplication”.

To get more help about aspnet_regiis use the below command
aspnet_regiis help