Jonathan Crossland
Posts: 630
Nickname: jonathanc
Registered: Feb, 2004
|
Jonathan Crossland is a software architect for Lucid Ocean Ltd
|
|
|
|
An ASP.NET web root link handler
|
Posted: Aug 20, 2009 4:48 AM
|
|
In order to get a useful, all purpose, all use client-side href link back to root, from anywhere down the web folder structure, and paying attention to IIS6, IIS7, Cassini and other Visual Studio internal web hosting scenarious and of course the odd bug within ASP.NET and your own web site code, I created a handler for ~/.
Here is the html I wanted.
<a href="~/">Some link to root of site</a>
public class RootHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
string toUrl = context.Request.ApplicationPath;
context.Response.Redirect(toUrl);
context.Response.End();
}
#endregion
}
<handlers>
<remove name="root"/>
<add name="root" verb="*" path="~/" type="Namespace.RootHandler, Assembly"/>
</handlers>
Then with a little more code in the ProcessRequest method, you could find the ~/ part of the url, strip it down and modify the url.
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
string toUrl = context.Request.ApplicationPath + "/";
string url = context.Request.RawUrl;
int idx = url.LastIndexOf("~/");
url = url.Substring(idx + 2);
url = VirtualPathUtility.Combine(toUrl, url);
context.Response.Redirect(url);
context.Response.End();
}
It is only with .NET Framework 3.5, where HttpHandlers have gotten to an acceptable bug free level for use.
Warning: this is a simplistic example for purpose of this post, please be careful in production scenarios.
Read: An ASP.NET web root link handler
|
|