Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HyperLink NavigateUrl rewrite

Reply
Thread Tools

HyperLink NavigateUrl rewrite

 
 
Tomas
Guest
Posts: n/a
 
      09-15-2009
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); } }


 
Reply With Quote
 
 
 
 
Tomas
Guest
Posts: n/a
 
      09-15-2009
\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);
}

}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP Hyperlink with absolute NavigateURL? is it possible Amir Eshterayeh ASP .Net 5 02-13-2009 05:12 PM
hyperlink and dynamically formed NavigateURL problem Tomek R. ASP .Net 7 02-13-2009 05:12 PM
HyperLink.NavigateUrl property not marked as virtual Cynic07 ASP .Net 0 05-20-2004 09:23 PM
Hyperlink NavigateUrl Mark Fox ASP .Net 5 11-04-2003 08:21 AM
undesired behavior in setting hyperlink .NavigateURL property Boban Dragojlovic ASP .Net 3 10-29-2003 09:05 PM



Advertisments