Outras Formas De Fazer Com Que PathInfo E ASP.NET Themes Coexistam
Na minha última entrada apresentei uma solução para os problemas que ocorrem quando tentamos usar path infos em conjunto com ASP.NET Themes and Skins.
O Raj Kaimal sugeriu reescrever os URLs de todos os elementos HTML LINK de forma a que fossem correctamente vistos pelo cliente. Algo como isto:
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);
}
}
};
}
}
Com esta aproximação persiste ainda o problema (que a minha aproximação não resolvia) com post backs porque o URL contido no atributo ACTION FORM HTML também tem os seus probelmas.
O Israel Aéce sugeriu a utilização do elemento HTML BASE para redefinir a base dos URLs relativos. Algo como isto:
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);
};
};
}
}
Esta parece ser a melhor solução excepto se o vosso site estiver por detrás de vários perímetros e de segurança e não for possível determinar com certeza qual o domínio tal como é visto do lado do cliente, que era o meu problema.
Mas se estão a pensar chamar Server.Execute, Server.TransferRequest ou Server.TransferRequest, nenhuma destas soluções funcionará.