HyperLink NavigateUrl rewrite

Discussion in 'ASP .Net' started by Tomas, Sep 15, 2009.

  1. Tomas

    Tomas Guest

    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); } }
     
    Tomas, Sep 15, 2009
    #1
    1. Advertising

  2. Tomas

    Tomas Guest

    code re-post

    \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);
    }

    }
     
    Tomas, Sep 15, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boban Dragojlovic
    Replies:
    3
    Views:
    3,047
    vMike
    Oct 29, 2003
  2. Mark Fox

    Hyperlink NavigateUrl

    Mark Fox, Nov 3, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    9,324
    Mark Fox
    Nov 4, 2003
  3. Amir Eshterayeh
    Replies:
    5
    Views:
    9,360
    Sobot
    Feb 13, 2009
  4. Cynic07
    Replies:
    0
    Views:
    424
    Cynic07
    May 20, 2004
  5. Tomek R.
    Replies:
    7
    Views:
    8,204
    Sobot
    Feb 13, 2009
Loading...

Share This Page