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.

  1. <assemblies>  
  2.   <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  3. </assemblies>  
  4. <httpModules>  
  5.   <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>  
  6.   <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  7. </httpModules>  
  8. <system.webServer>  
  9.   <validation validateIntegratedModeConfiguration="false"/>  
  10.   <modules runAllManagedModulesForAllRequests="true">  
  11.     <remove name="ScriptModule"/>  
  12.     <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  13.     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  14.   </modules>  
  15.   <handlers>  
  16.     <remove name="WebServiceHandlerFactory-Integrated"/>  
  17.     <remove name="ScriptHandlerFactory"/>  
  18.     <remove name="ScriptHandlerFactoryAppServices"/>  
  19.     <remove name="ScriptResource"/>  
  20.     <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"/>  
  21.     <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"/>  
  22.     <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"/>  
  23.     <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>  
  24.   </handlers>  
  25. </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)
    • MyRouteHandler.cs
    • Routing Folder (Idisplay.cs,IuserDisplay.cs

404.xml

  1. <Record>  
  2.   <oldurl>hello1.aspx</oldurl> --- dead url  
  3.   <newurl>google.aspx</newurl> --- redirecting url.

Important sentences in Global.asax

  1. <%@ Import Namespace="System.Web.Routing" %>  
  2. void Application_Start(object sender, EventArgs e)  
  3. {  
  4. RegisterRoutes(); -- important  
  5. }  
  6. private static void RegisterRoutes()  
  7. {  
  8. System.Web.Routing.RouteTable.Routes.Clear();  
  9. HttpContext context = HttpContext.Current;  
  10. System.Data.DataSet ds = new System.Data.DataSet();  
  11. ds.ReadXml(HttpContext.Current.Server.MapPath("~/404.xml"));  
  12. System.Data.DataTable dt = new System.Data.DataTable();  
  13. System.Web.Routing.RouteTable.Routes.Clear();  
  14. dt = ds.Tables[0];  
  15. foreach (System.Data.DataRow row in dt.Rows)  
  16. { System.Web.Routing.RouteTable.Routes.Add(Guid.NewGuid().ToString(), new System.Web.Routing.Route(  
  17. row[0].ToString(), new HavenRouteHandler(row[0].ToString().Trim(), row[1].ToString().Trim())));  
  18. }}  
  19. public class MyRouteHandler : IRouteHandler  
  20. {  
  21. public string NewUrl = "",OldUrl="";  
  22. public MyRouteHandler(string oldUrl,string newUrl)  
  23. {  
  24. NewUrl = newUrl;  
  25. OldUrl = oldUrl;  
  26. }  
  27. public IHttpHandler GetHttpHandler(RequestContext requestContext)  
  28. {  
  29. // string removedBreaks = Line.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");  
  30. HttpContext context = HttpContext.Current;  
  31. string url = context.Request.Url.ToString();  
  32. if (url.Contains(OldUrl))  
  33. {  
  34. // return BuildManager.CreateInstanceFromVirtualPath( "~/"+NewUrl, typeof(Page)) as Page;  
  35. return BuildManager.CreateInstanceFromVirtualPath("~/" + NewUrl, typeof(Page)) as Page;  
  36. }  
  37. else  
  38. {  
  39. return null;  
  40. }  
  41. }  
  42. }  
  43. public interface IDisplay : IHttpHandler  
  44. {  
  45. }  
  46. public interface IUserDisplay : IHttpHandler  
  47. {  
  48. string UniqueName { getset; }  
  49. } 

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.


Similar Articles