Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Redirector Module

, 19 Jan 2010 CPOL
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)

Share

About the Author

Jorge Bay Gondra
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
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 19 Jan 2010
Article Copyright 2010 by Jorge Bay Gondra
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid