Web Development

Creating 301 redirects in web.config

For various reasons at times you may need to create a 301 redirect to another URL. This could be as a result of a page moving or you just need to create some friendly URLS.

As a developer you may be tempted to do something like this in code...

1private void Page_Load(object sender, System.EventArgs e)
2{
3 Response.Status = "301 Moved Permanently";
4 Response.AddHeader("Location","http://www.new-url.com");
5}

But do you really want your project cluttered up with files who's only purpose is to redirect to another page!

You may also be tempted to try doing something with .NET's RouteCollection. This would certainly solve an issue on creating a redirect for anything without a file extension, but there is a better way.

In your web.config file under the configuration node create something like this

1<location path="twitter">
2 <system.webServer>
3 <httpRedirect enabled="true" destination="http://twitter.com/TwitterName" httpResponseStatus="Permanent" />
4 </system.webServer>
5</location>

The location path specifies that path on your site that this redirect will apply to. The destination value in the httpRedirect is where the redirect will go to. As well as setting Permanent for the httpResponseStatus you can also specify Found or Temporary depending on your needs.

ASP.NET Session Timeout

A users session on an ASP.NET site by default will time-out after 20 minutes. This however can be changed through either the web.config file or IIS.

To edit through the web.config file you need to edit the sessionState tag under system.web

1<system.web>
2 <sessionState timeout="30"></sessionState>
3</system.web>

Or through IIS click on your site name and then click Session State under the ASP.NET heading. There will be a field labeled Time-out (in minutes).

The value you enter for time-out must be an integer.

Help it doesn't seem to work!

If your sessions still seem like there timing out after 20 minutes it could be because your site isn't very active.

The application pool for your site also has an idle time-out that is set by default to 20 minutes. When the idle time-out is reached it will cause your application pool to recycle and therefore loose any active sessions (that's assuming you have the session state mode set to In Proc). Therefore it is a good idea to increase this to whatever you have set the session time-out to.

To do this go to your sites application pool in IIS, click advanced settings on the right and then look for the Idle Time-out (minutes) setting and update this to be the same as your session time-out value.

Caching data using static objects

One classic scenario of website speed issues relates to retrieving data from a database and often it is the same data being retrieved over and over again. For example storing some of your site settings in the database was originally a great idea as it meant your admin users could configure them through an admin, but now every request from your real users is doing a database call to look up this information.

A way to reduce these database calls is to create a static object that will keep hold of the information once its been retrieved. This could simply be an int or string or it could be something more complex like a List or Dictionary that we keep adding to.

Lets use an example where we are retrieving details of a room. The room can have multiple properties including Name, Capacity and Description. Or database contains multiple instances of rooms that is repeatedly requested.

Rather than accessing the database directly we can create a function that will first look up the value in a List object and if it is not found then revert to loading the information from the database. If the information was retrieved from a database then we also add it to the list for future use.

code for looking retrieving the data

As the List is a static object it will continue to hold the information of the rooms for the life of the application. Some things to watch out for however:

  • Static objects will be the same for all users, they are not related to a session
  • Static objects are accessible across all threads so you need to be aware that anything you change in the static object will be changing on all threads
  • When you return the object you wanted you will most likely be returning a reference to the existing object not just its value

With these things in mind, whenever you are updating the list (adding, removing etc) make sure you create necessary locks so that only 1 thread executes at a time. In the example this has been done when we check for the existence of the item and then go to load it. Without the lock it would be possible for 2 threads to do the check and then both go to load the values. If this were to happen we would end up with 2 instances of room in our list. Even worse if we were using a dictionary instead of a list the second instance would cause an exception when it tried to add it to the dictionary. With the lock the second thread will wait for the first to finish executing before it runs the code.