Postback problem when using URL Rewrite and 404.aspx

纵饮孤独 提交于 2019-12-05 22:37:09
form1.Action = Request.RawUrl

in combination with

HttpContext.Current.RewritePath("/Default.aspx", true); 

works very well for me.

setting the form Action attribute was the piece i was missing...

Thanks for the solution!!!

Scott Guthrie covers different ways of doing this here without IIS access:

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

Personally, I've created HTTPModules in the past and it's pretty easy to put together.

form1.Action = Request.RawUrl is also how it is written in C# put it in the page load method

I would start by installing Live Http Headers and see whats going on with the Http requests.

Ahh here in that article it explains it, looks like the action attribute of the form tag is wrong:

One gotcha that people often run into when using ASP.NET and Url-Rewriting has to-do with handling postback scenarios. Specifically, when you place a <form runat="server"> control on a page, ASP.NET will automatically by default output the "action" attribute of the markup to point back to the page it is on. The problem when using URL-Rewriting is that the URL that the control renders is not the original URL of the request (for example: /products/books), but rather the re-written one (for example: /products.aspx?category=books). This means that when you do a postback to the server, the URL will not be your nice clean one.

With ASP.NET 1.0 and 1.1, people often resorted to sub-classing the <form> control and created their own control that correctly output the action to use. While this works, it ends up being a little messy - since it means you have to update all of your pages to use this alternate form control, and it can sometimes have problems with the Visual Studio WYSIWYG designer.

The good news is that with ASP.NET 2.0, there is a cleaner trick that you can use to rewrite the "action" attribute on the <form> control. Specifically, you can take advantage of the new ASP.NET 2.0 Control Adapter extensibility architecture to customize the rendering of the <form> control, and override its "action" attribute value with a value you provide. This doesn't require you to change any code in your .aspx pages. Instead, just add a .browser file to your /app_browsers folder that registers a Control Adapter class to use to output the new "action" attribute...

You could just use the below instead:

form1.Action = Request.RawUrl

I'm not sure what that would be in C#, but this is what it is in VB and it works a treat

try something like this:

            if (Request.HttpMethod == "GET" && shouldChangeUrl)
            {
                urlRedirect = "REAL-URL-HERE";

                _postBackUrl = urlRedirect;
                Context.RewritePath(urlRedirect);
            }
            else
                //If Post Back (Request.HttpMethod="POST")
                Context.RewritePath(_postBackUrl);

So, you save the real URL for the postback in a variable (here: _postBackUrl) when rewriting the url and then use it only when postback.

The problem is within the 404 handling. Your form is posting to a non-exising page, ASP.NET redirects the request to the 404 page and so it loses all the postback data.

The only solution is to set the form's action attribute to the existing page, so users will see the index.aspx?user=john when they submit the form. For SEO this would not be a problem, since crawlers don't issue posts and therefore do not see the ugly address.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!