Blog

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.

Windows Phone: Sharing Content

If you writing an app and want to add some social sharing capabilities like posting link to Facebook or twitter. Your initial approach may be to go to and check out each of their api’s or to go to codeplex and search for a C# api someone’s written that makes the integration simpler. But right from that start Windows Phone has had a much simpler way of doing things.

The ShareLinkTask provides a simple way to post messages to any social network the user has registered on their phone. You don’t need to do anything with authorising your app with facebook etc which not only makes things easier for you, but your users are also pleased as they don’t have to worry about what you may be doing with their passwords.

It also only takes 4 lines of code. Simply create an instance of the ShareLinkTask, set the tile and message and call the show function:

1ShareLinkTask shareLinkTask = new ShareLinkTask();
2shareLinkTask.LinkUri = new Uri("http://www.himynameistim.com");
3shareLinkTask.Message = "Check out this great blog";
4shareLinkTask.Show();

As well as the ShareLinkTask there is also a ShareStatusTask and ShareMediaTask that can be used if you just want to post a status update or post and image.

1ShareStatusTask shareStatusTask = new ShareStatusTask();
2shareStatusTask.Status = "I'm developing a Windows Phone application!";
3shareStatusTask.Show();
4
5ShareMediaTask shareMediaTask = new ShareMediaTask();
6shareMediaTask.FilePath = path;
7shareMediaTask.Show();

Social media isn’t the only way of sharing content on a phone though, there is also email and sms. Both of these are just as easy to do as social media though:

For email use the EmailComposeTask

1EmailComposeTask emailComposeTask = new EmailComposeTask();
2emailComposeTask.Subject = "Awesome website";
3emailComposeTask.Body = "Foo bla bla";
4emailComposeTask.Show();

And for SMS use the SmsComposeTask

1SmsComposeTask smsComposeTask = new SmsComposeTask();
2
3smsComposeTask.To = "2065550123";
4smsComposeTask.Body = "Try this new application. It's great!";
5
6smsComposeTask.Show();

For more information have a look at the MSDN documentation:

Share Link Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394009(v=vs.105).aspx

Share Status Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394027(v=vs.105).aspx

Share Media Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207027(v=vs.105).aspx

Email Componse Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394003(v=vs.105).aspx

SMS Compose Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394005(v=vs.105).aspx

Windows Phone: Rate My App

If you want your app to succeed one thing you need is reviews. More importantly you need good reviews!

Stats show that if an app has a rating of 4 stars or above it will do significantly better than an app with less. The higher the number of reviews, the more believable that rating is.

One issue you may have though is how to get reviews. Sadly most users don't rate apps unless they really like it or hate it. The Windows Phone OS also does nothing to prompt people to rate apps, so if you want to build up reviews then your going to need to do the prompting yourself.

The code to direct someone to the review page in the store for your app is quite simple. But even better is the fact Nokia have produced sample code that does the whole process!

Once you've integrated the code on the 5th launch of your app the user will see a prompt asking them to rate your app. If they choose not to then they will be prompted to send you feedback via email.

If they didn't rate your app then on the 10th launch they will be prompted again.

Now getting good reviews is slightly harder, for that your actually going to have to build a good app that people like.

You can download the Rate My App code from Nokia here.

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.