Share via


ASP.NET: Development Tips And Tricks Part 2 - Cookieless Sessions, Smart Navigation and AppSettings

Many developers out there state that they don’t want to use cookies. People get confused about cookies such as permanent cookies for malicious reasons and things such as session cookies. Session cookies are not really storing any thing personal about the user. They’re just storing a session key to be used to get back to session data stored on the server. This is a big problem today. If a client has cookies disabled, your application could break.

With ASP.NET you can now optionally select to support cookieless sessions. ASP.NET can track the SessionID in the URL instead of storing it as a cookie. ASP.NET does all of this behind the scenes for you by inserting a SessionID directly into the URL.

The great thing is that this doesn’t require any code changes to your application. As long as you have relative links, everything will work.

Create “web.config” file in app vroot

Add following text:

<configuration>

<system.web>

<sessionState cookieless=“true”/>

</system.web>

</configuration>

Wouldn’t it be great to have all the benefits of client side applications such as smooth scrolling and no screen flicker, but have all of your code running on the server? ASP.NET has added a new featured called Smart Navigation to help with this problem. It doesn’t require any new code changes for it to work. Instead, what you do is at the top of the page directive, you set the SmartNavigation = true.

No client code changes required

<%@ Page SmartNavigation=“true” %>

Alternatively set in web.config file

What that will do is when a client that has DHTML script enabled hits the page, on postbacks to that page, ASP.NET detects what the differences are in the page that they have and in what they’re getting. Then the client can intelligently repaint only the portions of the page that have changed. The nice thing is that if you hit this with a non-IE browser or a browser that does not have DHTML script enabled, it will automatically degrade. There’s not a lot of downside to using this. Smart navigation works with IE 5 and above.

App Settings is great tip and trick to point out. There are many developers that hard-code things into their applications, such as path locations, database connection strings, and MSMQ locations. It’s easy, it’s simple, but it’s also problematic if not done right. The good news is that ASP.NET makes this very easy to do. No longer do you need to put resource string directly into your code, but now you can put them in one clean place called Web.Config. This is a configuration file that is used to configure an ASP.NET application on the server. Also, there’s a very simple API that you can use to get access to this file. It’s called Configuration.AppSettings.

1. Create “web.config” file in app vroot:

<configuration>

<appSettings>

<add key=“dsn”

value=“localhost;uid=sa;pwd=;Database=foo”/>

</appSettings>

</configuration>

2. To return value:

Configuration.AppSettings(“dsn”)

This is what the XML config file looks like. You just need to add this file to your applications root. You can add as many settings as you want.

In your code, just call Configuration.AppSettings and pass in the setting that you want returned and it will automatically retrieve the setting for you. The neat thing is that this Web.Config file is cached in memory. So, the first time you read this file it will be read into memory, but then from there on, it’s all in memory, so it’s very fast.

Another great reason to use this file is let’s say your system administrator decides to change the SQL password for your database. By using Web.Config, they don’t need to call you, they can just change the value in the Web.Config file and immediately your code will pick up the differences.

One cool thing is that this can be used outside of ASP.NET. It can be used by any component called from ASP.NET.