I created a Url Rewriting solution based on some info I found on several post.
However, when I do a postback on one of the pages (click a submit button for instance) the underlying url is used, and the rewriting fails.
Any idea how I can solve this ?
this is in my Application_BeginRequest
strCurrentPath = Request.Path;
strCurrentPath = strCurrentPath.ToLower();
strCustomPath = UrlRewriter.GetURL(strCurrentPath);
Context.RewritePath(strCustomPath);
the urlrewriter simply checks the syntax of the url and changes it into the correct on, based on information coming from the database.
example :
public static string GetURL(string currentpath)
{
string strCurrentPath = currentpath.ToLower();
string strCustomPath;
if (strCurrentPath.IndexOf("/articles/") > -1)
{
strCustomPath = "~/Articles.aspx?ID=" + System.IO.Path.GetFileNameWithoutExtension(strCurrentPath);
Thanks for the link! However, using Scott's HtmlForm class, I'm now having problems with the server controls that reside on the page. The state of the server control doesn't appear to persist on postback.
For instance:
<asp:DropDownList id="ddl_Test" AutoPostBack="true" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
If I select '2', the postback occurs, but then the dropdownlist gets reset to '1'. Any suggestions?
Scott's solution is not complete because it does not render all of the form tag's possible attributes, especially the OnSubmit attribute, which is set via Page.ClientScript.RegisterOnSubmitStatement (in 2.0). I would suspect that this is causing your problem.
It is not easy to render in the overridden HtmlForm class because it is accessalble only via internal methods on Page. There are at least two options for a complete solution:
1. Create a overridden HtmlTextWriter that strips out (or better yet replaces) the action attribute. This writer can be inserter into the process in the RenderChildren method of a overridden Page class. Since there are at least 2 different HtmlTextWriter
implementations, based on the browser's capabilities, the easiest solution I found was to create a proxied writer (via RealProxy). The performance hit is miniminal since it is only used to write out the Form control's rendering.
2. Create a response stream wrapper (set via the Context.Response.Filter property) that parses the output text and replaces the action attribute. Depending on how you do the parsing, this can be fairly expensive. One advantage of this appropach is that
it can be set inside the URL mapping module, making it transparent to the rest of the app.
My mapping solution actually uses both of these techniques, the first for most pages, the second as a fallback for any pages that aren't based on the correct class. Some fairly simple switching logic ensures that the highest performance path normally operates.
It works well an anything that I have tried to render- which includes a pretty large range of standard and custom controls, including some AJAX stuff.
None
0 Points
47 Posts
Url rewriting & postback
Nov 17, 2005 05:14 AM|Koen|LINK
Hi,
I created a Url Rewriting solution based on some info I found on several post.
However, when I do a postback on one of the pages (click a submit button for instance) the underlying url is used, and the rewriting fails.
Any idea how I can solve this ?
this is in my Application_BeginRequest
strCurrentPath = Request.Path;
strCurrentPath = strCurrentPath.ToLower();
strCustomPath = UrlRewriter.GetURL(strCurrentPath);
Context.RewritePath(strCustomPath);
the urlrewriter simply checks the syntax of the url and changes it into the correct on, based on information coming from the database.
example :
public static string GetURL(string currentpath)
{
string strCurrentPath = currentpath.ToLower();
string strCustomPath;
if (strCurrentPath.IndexOf("/articles/") > -1)
{
strCustomPath = "~/Articles.aspx?ID=" + System.IO.Path.GetFileNameWithoutExtension(strCurrentPath);
// rewrite the URL
return strCustomPath;
}
thanks in advance
koen
Member
21 Points
74 Posts
ASPInsiders
MVP
Re: Url rewriting & postback
Nov 17, 2005 05:25 PM|ScottCate|LINK
This topic has been exsaulsted in many places, but I'll point you to my favorite article from our very own Scott Mitchell.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp
Good Luck.
Scott Cate - myKB.com
Knowledge Base / FAQ Support Manager
Knowledge Base Software
None
0 Points
9 Posts
Re: Url rewriting & postback
Dec 02, 2005 08:56 PM|dontmindme|LINK
For instance:
<asp:DropDownList id="ddl_Test" AutoPostBack="true" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
If I select '2', the postback occurs, but then the dropdownlist gets reset to '1'. Any suggestions?
Thanks!
None
0 Points
9 Posts
Re: Url rewriting & postback
Dec 06, 2005 09:18 PM|hartwg|LINK
Scott's solution is not complete because it does not render all of the form tag's possible attributes, especially the OnSubmit attribute, which is set via Page.ClientScript.RegisterOnSubmitStatement (in 2.0). I would suspect that this is causing your problem.
It is not easy to render in the overridden HtmlForm class because it is accessalble only via internal methods on Page. There are at least two options for a complete solution:
1. Create a overridden HtmlTextWriter that strips out (or better yet replaces) the action attribute. This writer can be inserter into the process in the RenderChildren method of a overridden Page class. Since there are at least 2 different HtmlTextWriter implementations, based on the browser's capabilities, the easiest solution I found was to create a proxied writer (via RealProxy). The performance hit is miniminal since it is only used to write out the Form control's rendering.
2. Create a response stream wrapper (set via the Context.Response.Filter property) that parses the output text and replaces the action attribute. Depending on how you do the parsing, this can be fairly expensive. One advantage of this appropach is that it can be set inside the URL mapping module, making it transparent to the rest of the app.
My mapping solution actually uses both of these techniques, the first for most pages, the second as a fallback for any pages that aren't based on the correct class. Some fairly simple switching logic ensures that the highest performance path normally operates.
It works well an anything that I have tried to render- which includes a pretty large range of standard and custom controls, including some AJAX stuff.
Member
132 Points
435 Posts
Re: Url rewriting & postback
Dec 07, 2005 02:36 PM|AndrewSeven|LINK
You could try using an IHttpHandlerFactory
http://weblogs.asp.net/andrewseven/articles/UrlRewriting.aspx
Member
2 Points
20 Posts
Re: Url rewriting & postback
Dec 08, 2005 06:19 AM|zootius|LINK
Have a look here:
http://odetocode.com/Blogs/scott/archive/2004/09/22/509.aspx
Seems to be the second RewritePath that you're missing?