URL Redirection on ASP.Net 3.5

URL Redirection on ASP.Net 3.5

URL rewriting in .Net after version 2010 is as easy as in .Net Framework 4.0 and above with a built-in capability for handling URL rewriting or redirection. Since the version of .Net used in Tridion 2009 is the Framework version 3.5. A global.asax file needs to be created and appended to the web config. By default the Framework 3.5 won't reference the system.web.Routing assemblies. So there is a need to reference the System.Web.Routing assembly to the site. The location of the library is C:\Windows\assembly.

<assemblies>

  <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</assemblies>

<httpModules>

  <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpModules>

<system.webServer>

  <validation validateIntegratedModeConfiguration="false"/>

  <modules runAllManagedModulesForAllRequests="true">

    <remove name="ScriptModule"/>

    <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  </modules>

  <handlers>

    <remove name="WebServiceHandlerFactory-Integrated"/>

    <remove name="ScriptHandlerFactory"/>

    <remove name="ScriptHandlerFactoryAppServices"/>

    <remove name="ScriptResource"/>

    <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

  </handlers>

</system.webServer>


Procedure

  1. Copy the global.asax file to the route of the application.
  2. Copy the 404 XML file to the route of the application
  3. Copy the following files to the application folder (App_code)

    1. MyRouteHandler.cs
    2. Routing Folder (Idisplay.cs,IuserDisplay.cs

404.xml

<Record>

  <oldurl>hello1.aspx</oldurl> --- dead url

  <newurl>google.aspx</newurl> --- redirecting url.


Important sentences in Global.asax

<%@ Import Namespace="System.Web.Routing" %>

void Application_Start(object sender, EventArgs e)

{

 

RegisterRoutes(); -- important

}

private static void RegisterRoutes()

{

System.Web.Routing.RouteTable.Routes.Clear();

HttpContext context = HttpContext.Current;

System.Data.DataSet ds = new System.Data.DataSet();

ds.ReadXml(HttpContext.Current.Server.MapPath("~/404.xml"));

System.Data.DataTable dt = new System.Data.DataTable();

System.Web.Routing.RouteTable.Routes.Clear();

dt = ds.Tables[0];

foreach (System.Data.DataRow row in dt.Rows)

{ System.Web.Routing.RouteTable.Routes.Add(Guid.NewGuid().ToString(), new System.Web.Routing.Route(

row[0].ToString(), new HavenRouteHandler(row[0].ToString().Trim(), row[1].ToString().Trim())));

}}

public class MyRouteHandler : IRouteHandler

{

public string NewUrl = "",OldUrl="";

public MyRouteHandler(string oldUrl,string newUrl)

{

NewUrl = newUrl;

OldUrl = oldUrl;

}

public IHttpHandler GetHttpHandler(RequestContext requestContext)

{

// string removedBreaks = Line.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");

HttpContext context = HttpContext.Current;

string url = context.Request.Url.ToString();

if (url.Contains(OldUrl))

{

// return BuildManager.CreateInstanceFromVirtualPath( "~/"+NewUrl, typeof(Page)) as Page;

return BuildManager.CreateInstanceFromVirtualPath("~/" + NewUrl, typeof(Page)) as Page;

}

else

{

return null;

}

}

}

public interface IDisplay : IHttpHandler

{

}

public interface IUserDisplay : IHttpHandler

{

string UniqueName { get; set; }

}

IIS Version 6.0

If the version of IIS is 6.0 then please use the following procedure.

Set validationIntegrateModeConfiguration to true on the web config file.

<validation validateIntegratedModeConfiguration="true"/>


It is also necessary to bypass the ISAPI filter by applying a wildcard.

Bypass the ISAPI filter

The following is the procedure to bypass the ISAPI filter:

  1. Application Configuration Properties of the website.
  2. Go to Mapping Tab
  3. Click on the Insert Tab.
  4. Select the aspnet_isapi.dll on the browse tab. (Normally the file sites on C:\Windows\Microsoft.NET\Framework\v2.0.50727 )
  5. Untick the Check box saying Verify that file Exists (This is very important, if it is ticked it won't work)
  6. Restart the site on IIS.

Up Next
    Ebook Download
    View all
    Learn
    View all