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;

    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)
        {
            var p = source as System.Web.UI.Page;

            foreach (System.Web.UI.Control headerControl in p.Header.Controls)
            {
                var link = headerControl as System.Web.UI.HtmlControls.HtmlLink;
                if (link != null)
                {
                    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.

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

  • Donna says:

    Thank you i post here:Type is not resolved for meembr  Site.Web.Models.SiteIdentity,Site.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Type is not resolved for meembr  Site.Web.Models.SiteIdentity,Site.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.Stack Trace: [SerializationException: Type is not resolved for meembr ‘Site.Web.Models.SiteIdentity,Site.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.]   Microsoft.VisualStudio.WebHost.Connection.get_RemoteIP() +0   Microsoft.VisualStudio.WebHost.Request.GetRemoteAddress() +65   System.Web.HttpRequest.get_IsLocal() +23   System.Web.Configuration.CustomErrorsSection.CustomErrorsEnabled(HttpRequest request) +86   System.Web.HttpContextWrapper.get_IsCustomErrorEnabled() +45   System.Web.Mvc.HandleErrorAttribute.OnException(ExceptionContext filterContext) +72   System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception) +115   System.Web.Mvc.Async.c__DisplayClass25.b__22(IAsyncResult asyncResult) +105   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +45   System.Web.Mvc.c__DisplayClass1d.b__18(IAsyncResult asyncResult) +14   System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +25   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +61   System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +25   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +49   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10   System.Web.Mvc.c__DisplayClassb.b__4(IAsyncResult asyncResult) +28   System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +25   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62   System.Web.Mvc.c__DisplayClasse.b__d() +50   System.Web.Mvc.SecurityUtil.b__0(Action f) +7   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

  • paulo says: