hits counter

Other Ways For Making PathInfo And ASP.NET Themes Work Together

On my last post I wrote about a solution for the problem that arises when we try the use path infos and ASP.NET Themes and Skins together.

Raj Kaimal suggested rewriting all LINK HTML elements URLs to the correct URL as seen from the client. Something like this:

void HttpApplicationPreRequestHandlerExecute(object sender, System.EventArgs e)
{
var httpApplication = sender as System.Web.HttpApplication;

<span style="color: blue">var </span>httpContext = httpApplication.Context;

<span style="color: blue">var </span>page = httpContext.CurrentHandler <span style="color: blue">as </span>System.Web.UI.<span style="color: #2b91af">Page</span>;

<span style="color: blue">if </span>((page != <span style="color: blue">null</span>) &amp;&amp; !<span style="color: blue">string</span>.IsNullOrEmpty(httpContext.Request.PathInfo))
{
    page.PreRenderComplete += <span style="color: blue">delegate</span>(<span style="color: blue">object </span>source, System.<span style="color: #2b91af">EventArgs </span>args)
    {
        <span style="color: blue">var </span>p = source <span style="color: blue">as </span>System.Web.UI.<span style="color: #2b91af">Page</span>;

        <span style="color: blue">foreach </span>(System.Web.UI.<span style="color: #2b91af">Control </span>headerControl <span style="color: blue">in </span>p.Header.Controls)
        {
            <span style="color: blue">var </span>link = headerControl <span style="color: blue">as </span>System.Web.UI.HtmlControls.<span style="color: #2b91af">HtmlLink</span>;
            <span style="color: blue">if </span>(link != <span style="color: blue">null</span>)
            {
                link.Href = p.ResolveUrl(link.Href);
            }
        }
    };
}

}

With this approach you still have a problem (which mine didn’t solve) with post backs because the rendering of the ACTION of the HTML FORM is also broken.

Israel Aéce suggested the use of the BASE HTML element to re-base relative URLs. Something like this:

void HttpApplicationPreRequestHandlerExecute(object sender, System.EventArgs e)
{
    var httpApplication = sender as System.Web.HttpApplication;

    var httpContext = httpApplication.Context;

    var page = httpContext.CurrentHandler as System.Web.UI.Page;

    if ((page != null) && !string.IsNullOrEmpty(httpContext.Request.PathInfo))
    {
        page.PreRenderComplete += delegate(object source, System.EventArgs args)
        {
            page.Init += delegate(object source, System.EventArgs args)
            {
                var p = source as System.Web.UI.Page;

                var htmlBase = new System.Web.UI.WebControls.WebControl(System.Web.UI.HtmlTextWriterTag.Base);
                htmlBase.Attributes.Add("href", p.Request.Url.GetLeftPart(System.UriPartial.Authority) + p.Request.CurrentExecutionFilePath);
                p.Header.Controls.Add(htmlBase);
            };
        };
    }
}

This seems like the better solution except if your site sits behind several security perimeters and it is not possible to be sure what the domain is as seem from the client side, which was my problem to begin with.

But if you are thinking of calling Server.Execute, Server.TransferRequest or Server.TransferRequest, neither of these two solutions will work.

No Comments