Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to rewrite a url in asp.net?
Posted
Comments
Rahul Rajat Singh 22-Feb-12 7:09am    
Could u please elaborate on what u want to do?
[no name] 22-Feb-12 7:10am    
You've got to do better than this. Try to write the question as if we have no idea what you are trying to accomplish

U don't Like Google?

rewrite a url in asp.net[^]
 
Share this answer
 
Comments
fjdiewornncalwe 22-Feb-12 11:32am    
Please use full words here. You've been around long enough to know that text speak isn't acceptable.
Perhaps u could find HttpServerUtility.UrlEncode or similar functions useful
 
Share this answer
 
Comments
fjdiewornncalwe 22-Feb-12 11:32am    
Please use full words here. You've been around long enough to know that text speak isn't acceptable.
FormRewriterControlAdapter.cs
C#
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 override void WriteAttribute(string name, string value, bool fEncode)
    {
        if (name == "action")
        {
            HttpContext Context;
            Context = HttpContext.Current;
            if (Context.Items["ActionAlreadyWritten"] == null)
            {
                value = Context.Request.RawUrl;
                Context.Items["ActionAlreadyWritten"] = true;
            }
        }
        base.WriteAttribute(name, value, fEncode);
    }
}

web.config
XML
<rewriter>
<rewrite url="/WebSite3/Product/(.+)" to="/WebSite3/Product.aspx?category=$1" />
<!--<rewrite url="/WebSite3/Product/(.+).aspx" to="/WebSite3/Product.aspx?category=$1"/>-->
<!--<rewrite url="/WebSite3/Product/books.aspx" to="/WebSite3/Product.aspx?category=books" />-->
</rewriter>


in App_Browser folder
Form.Browser file
XML
<browsers>
   <browser refid="Default">
      <controladapters>
         <adapter controltype="System.Web.UI.HtmlControls.HtmlForm">
	   adapterType="FormRewriterControlAdapter" />
         </adapter>
      </controladapters>
   </browser>
</browsers>
 
Share this answer
 
v2
Comments
fjdiewornncalwe 22-Feb-12 11:34am    
Just cleaned up the pre tags for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900