Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How ASP.NET and URL rewrite handle PostBack

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to deal with PostBack about ASP.NET and URL rewriting. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Handle PostBack in ASP.NET and URL rewrite

One of the problems that people often encounter when using ASP.NET and URL rewriting is related to dealing with postback scenarios. Specifically, when you place a control on a web page, ASP.NET automatically defaults to the action property of the logo that points to the current page. When using URL rewriting, the problem occurs that the URL displayed by the control is not the originally requested URL (for example, / products/books), but the rewritten URL (such as / products.aspx?category=books). This means that when you do a postback to the server, URL is no longer the neat one you used to be.

In ASP.NET 1.0 and 1.1, people often resorted to inheriting controls to generate their own controls to correctly output the action properties to be used. Although this works, the result is a bit messy because it means you need to update all your pages to use this additional form control, and sometimes you encounter problems in the Visual Studio WYSIWYG designer.

The good news is that in ASP.NET 2.0, there is a cleaner trick you can use to override the control's action property. Specifically, you can customize the control's output with the new ASP.NET 2.0 control adapter extension architecture, overriding the value of the action property with the value you provide. This does not require any coding changes in your .aspx page, but just add a .browser file to your / app_browsers folder and register to use a control adaptation class to output the new action properties.

Here you can see a sample implementation I created that shows how to implement a form control adapter (Form Control Adapter) that works with URL rewriting. It works in the * (Request.PathInfo) and the second method (UrlRewriter.Net module) I used above, and it uses the RawUrl property of Request to get the URL that has not been rewritten before to display. In the fourth method (ISAPIRewrite filter), you can get the original URL value that the ISAPI filter saved in Request.ServerVariables ["HTTP_X_REWRITE_URL"].

My FormRewriter class implementation above should work on both standard ASP.NET and ASP.NET AJAX 1.0 web pages (let me know if you have any problems).

Handle CSS and image references correctly

A problem that many people sometimes encounter when using URL rewriting is that they find that their images and CSS stylesheet references sometimes stop working. This is because they have relative references to these files in HTML pages, and when you start to rewrite URL in your application, you need to be aware that browsers often request files at different logical hierarchies (logical hierarchy levels), rather than what is actually stored on the server.

For example, if our / products.aspx page above has a relative reference to the logo.jpg in the .aspx page, but is requested through the URL of / products/books.aspx, then the browser will issue a request for / products/logo.jpg instead of / logo.jpg when displaying the page. To reference this file correctly, make sure you qualify (root qualify) CSS and image reference ("/ style.css", not "style.css") with the root directory. For ASP.NET controls, you can also use the "~" syntax to reference files (for example,) from the root directory of your application.

This is the end of the article on "ASP.NET and URL rewriting how to deal with PostBack". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report