HyperLink NavigateUrl rewrite

  • Thread starter Thread starter Tomas
  • Start date Start date
T

Tomas

Hello,

How to rewrite HyperLink NavigateUrl property? I want to add full URL with
domain name. To rewrite Images and other controls I use code below but it
seems do not work on HyperLink control.

regards,
Tomas


\App_Browsers\Form.browser <browsers> <browser refID="Default">
<controlAdapters> <adapter
controlType="System.Web.UI.HtmlControls.HtmlLink"
adapterType="LinkRewriterControlAdapter" /> <adapter
controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="FormRewriterControlAdapter" />
</controlAdapters> </browser> </browsers> \App_Code\FormRewriter.cs
using System;using System.Data;using System.Configuration;using
System.IO;using System.Net;using System.Web;using System.Web.Security;using
System.Web.UI;using System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using
System.Text; public class PathProvider{ public static string GetBaseURL()
{ string url = null; url =
HttpContext.Current.Request.Url.Scheme + "://" +
HttpContext.Current.Request.Url.Authority +
HttpContext.Current.Request.ApplicationPath; if ((url.EndsWith("/")))
{ return url.Remove(url.LastIndexOf("/")); } else
{ return url; } }} public class
LinkRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{ base.Render(new RewriteLinkHtmlTextWriter(writer)); } }public
class RewriteLinkHtmlTextWriter : HtmlTextWriter{ public
RewriteLinkHtmlTextWriter(HtmlTextWriter writer) : base(writer)
{ this.InnerWriter = writer.InnerWriter; } public
RewriteLinkHtmlTextWriter(System.IO.TextWriter writer) : base(writer)
{ base.InnerWriter = writer; } public override void
WriteAttribute(string name, string value, bool fEncode) { // If
the attribute we are writing is the "action" attribute, and we are not on a
sub-control, // then replace the value to write with the raw URL of
the request - which ensures that we'll // preserve the PathInfo value
on postback scenarios if ((name == "href")) {
HttpContext Context = default(HttpContext); string
sStylesheetName = null; int iSlashPos = 0; string
contextItemKey = null; Context = HttpContext.Current;
contextItemKey = value + "HrefAlreadyWritten"; if
(Context.Items[contextItemKey] == null) { sStylesheetName =
value; iSlashPos = value.LastIndexOf("../");
if (iSlashPos >= 0) { sStylesheetName =
value.Remove(iSlashPos,3); } value
=PathProvider.GetBaseURL() + "/" + sStylesheetName;
Context.Items[contextItemKey] = true; } }
base.WriteAttribute(name, value, fEncode); } } public class
FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{ base.Render(new RewriteFormHtmlTextWriter(writer)); } }public
class RewriteFormHtmlTextWriter : HtmlTextWriter{ public
RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer)
{ this.InnerWriter = writer.InnerWriter; } public
RewriteFormHtmlTextWriter(System.IO.TextWriter writer) : base(writer)
{ base.InnerWriter = writer; } public override void
WriteAttribute(string name, string value, bool fEncode) { // If
the attribute we are writing is the "action" attribute, and we are not on a
sub-control, // then replace the value to write with the raw URL of
the request - which ensures that we'll // preserve the PathInfo value
on postback scenarios if (name == "src") {
HttpContext Context = default(HttpContext); string sAppendValue =
null; int iSlashPos = 0; string contextItemKey = null;
Context = HttpContext.Current; contextItemKey = value +
"HrefAlreadyWritten"; if (Context.Items[contextItemKey] == null)
{ sAppendValue = value; iSlashPos =
value.LastIndexOf("../"); if (iSlashPos >= 0)
{ sAppendValue = value.Remove(iSlashPos,
} value = PathProvider.GetBaseURL() + "/" + sAppendValue;
Context.Items[contextItemKey] = true; } }
base.WriteAttribute(name, value, fEncode); } }
 
\App_Browsers\Form.browser

<browsers>

<browser refID="Default">
<controlAdapters>
<adapter
controlType="System.Web.UI.HtmlControls.HtmlLink"
adapterType="LinkRewriterControlAdapter" />
<adapter
controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="FormRewriterControlAdapter" />
</controlAdapters>
</browser>

</browsers>

\App_Code\FormRewriter.cs

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public class PathProvider
{
public static string GetBaseURL()
{
string url = null;
url = HttpContext.Current.Request.Url.Scheme + "://" +
HttpContext.Current.Request.Url.Authority +
HttpContext.Current.Request.ApplicationPath;
if ((url.EndsWith("/")))
{
return url.Remove(url.LastIndexOf("/"));
}
else
{
return url;
}
}
}

public class LinkRewriterControlAdapter :
System.Web.UI.Adapters.ControlAdapter
{

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(new RewriteLinkHtmlTextWriter(writer));
}

}
public class RewriteLinkHtmlTextWriter : HtmlTextWriter
{

public RewriteLinkHtmlTextWriter(HtmlTextWriter writer) : base(writer)
{
this.InnerWriter = writer.InnerWriter;
}

public RewriteLinkHtmlTextWriter(System.IO.TextWriter writer) :
base(writer)
{
base.InnerWriter = writer;
}



public override void WriteAttribute(string name, string value, bool
fEncode)
{

// If the attribute we are writing is the "action" attribute, and we
are not on a sub-control,
// then replace the value to write with the raw URL of the request -
which ensures that we'll
// preserve the PathInfo value on postback scenarios


if ((name == "href"))
{

HttpContext Context = default(HttpContext);
string sStylesheetName = null;
int iSlashPos = 0;
string contextItemKey = null;
Context = HttpContext.Current;
contextItemKey = value + "HrefAlreadyWritten";

if (Context.Items[contextItemKey] == null) {


sStylesheetName = value;
iSlashPos = value.LastIndexOf("../");
if (iSlashPos >= 0)
{
sStylesheetName = value.Remove(iSlashPos,3);
}
value =PathProvider.GetBaseURL() + "/" + sStylesheetName;


Context.Items[contextItemKey] = true;

}
}



base.WriteAttribute(name, value, fEncode);
}

}






public class FormRewriterControlAdapter :
System.Web.UI.Adapters.ControlAdapter
{

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}

}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{

public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
this.InnerWriter = writer.InnerWriter;
}

public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}


public override void WriteAttribute(string name, string value, bool
fEncode)
{

// If the attribute we are writing is the "action" attribute, and we
are not on a sub-control,
// then replace the value to write with the raw URL of the request -
which ensures that we'll
// preserve the PathInfo value on postback scenarios



if (name == "src")
{

HttpContext Context = default(HttpContext);
string sAppendValue = null;
int iSlashPos = 0;
string contextItemKey = null;
Context = HttpContext.Current;
contextItemKey = value + "HrefAlreadyWritten";

if (Context.Items[contextItemKey] == null)
{


sAppendValue = value;
iSlashPos = value.LastIndexOf("../");
if (iSlashPos >= 0)
{
sAppendValue = value.Remove(iSlashPos, 3);
}
value = PathProvider.GetBaseURL() + "/" + sAppendValue;


Context.Items[contextItemKey] = true;

}
}



base.WriteAttribute(name, value, fEncode);
}

}
 
Back
Top