Click here to Skip to main content
15,896,118 members
Articles / Web Development / ASP.NET

Redirector Module

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
19 Jan 2010CPOL2 min read 36.1K   35  
Build an ASP.NET HttpModule to have normalized URLs, and avoid duplicate content for a SEO friendly website.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SampleRedirector.Tests.Fakes;
using SampleRedirector.Modules;
using SampleRedirector.Configuration;
using System.Configuration;

namespace SampleRedirector.Tests
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class RedirectorModuleTests
	{
		public RedirectorModuleTests()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Folder redirection
		[TestMethod]
		public void FolderRedirection_Test()
		{
			//SEE THE APP.CONFIG FILE

			FakeHttpContext context = null;
			//Configuration from Test project's App.config file
			RedirectorConfiguration config = RedirectorConfiguration.Current;
			RedirectorModule module = new RedirectorModule();

			//The folder itself
			context = new FakeHttpContext("http://contoso.com/press-releases");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == "http://contoso.com/press");

			//The child item inside the folder
			context = new FakeHttpContext("http://contoso.com/press-releases/child-detail1.aspx");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == "http://contoso.com/press/child-detail1.aspx");
		}
		#endregion

		#region Item redirection
		[TestMethod]
		public void ItemRedirection_Test()
		{
			//SEE THE APP.CONFIG FILE
			FakeHttpContext context = null;
			RedirectorConfiguration config = RedirectorConfiguration.Current;
			RedirectorModule module = new RedirectorModule();

			context = new FakeHttpContext("http://contoso.com/news-items/index.html");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == "http://contoso.com/news-items/");

			context = new FakeHttpContext("http://contoso.com/news-items/article250.html");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == "http://contoso.com/news-items/interesting-article250.aspx");
		} 
		#endregion

		#region Domain Redirection test
		[TestMethod]
		public void DomainRedirection_Test()
		{
			//SEE THE APP.CONFIG FILE
			FakeHttpContext context = null;
			RedirectorConfiguration config = RedirectorConfiguration.Current;
			RedirectorModule module = new RedirectorModule();

			//Test the www. subdomain remove
			context = new FakeHttpContext("http://www.contoso.com/testing/");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == "http://contoso.com/testing/");

			//Test that a url without the www. is not redirected
			context = new FakeHttpContext("http://contoso.com/testing/");
			module.RedirectRequest(context, config);
			Assert.IsTrue(context.Response.Headers["Location"] == null);
		} 
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Spain Spain
Jorge has been working in Software development for more than 10 years. Born in Argentina, he lives in Spain since 2004.

He worked as a consultant for mayor companies including Log, HP and Avanade and holds some technical certifications including MCSD and MCAD.

He is the founder of the asp.net mvc forum open source project Nearforums, the Node.js Cassandra driver and the owner of the news release site prsync.com.

Follow him on Twitter: twitter.com/jorgebg

Contact: jorgebaygondra at gmail

Comments and Discussions