ASP. NET and URL rewriting

Source: Internet
Author: User

Processing PostBack in ASP. NET and URL rewriting

A difficult issue that people often encounter when using ASP. NET and URL rewriting is related to processing postback scenarios. Specifically, when you place a <form runat = "server"> control on a webpage, ASP. NET automatically points the action attribute of the output identifier to the current page by default. This problem occurs when URL rewriting is used. The <form> Control shows that the URL is not the original requested URL (for example,/products/books ), instead, the URL (for example,/products. aspx? Category = books ). This means that when you make a postback to the server, the URL is no longer the one you used to clean up.

In ASP. NET 1.0 and 1.1, you often resort to inheriting the <form> controls to generate their own controls to correctly output the action attributes to be used. Although this works, the results are messy, because it means you need to update all your pages to use this other form control, and sometimes there are problems in the WYSIWYG designer in Visual Studio.

The good news is that in ASP. NET 2.0, there is a cleaner trick you can use to override the action attribute of the <form> control. Specifically, you can use the new ASP. NET 2.0 control adapter extension architecture to customize the control output and overwrite the value of the action property with the value you provide. This is not required in your. make any encoding changes on the aspx page, and add one in your/app_browsers folder. browser file, register and use a control adaptation class to output new action attributes.

You can view a sample implementation that I created here, which shows how to implement the Form Control Adapter (Form Control Adapter) that cooperates with URL rewriting ). The first Request. pathInfo), the second method (UrlRewriter.. Net module), it uses the RawUrl attribute of the Request to obtain the URL that has not been rewritten for display. In the fourth method (ISAPIRewrite filter), you can obtain the original URL value of the ISAPI filter stored in Request. ServerVariables ["HTTP_X_REWRITE_URL.

The FormRewriter class above should work on standard ASP. NET and ASP. net ajax 1.0 web pages (if you have any problems, let me know ).
Correctly process CSS and image references

When using URL rewriting for the first time, many people sometimes encounter a difficult problem, that is, they find that their images and CSS style sheet references sometimes stop working. This is because they have relative references to these files on HTML pages. When you start to rewrite the URL in the application, you need to realize that browsers often request files at different logical hierarchy levels layers, rather than what is actually stored on the server.

For example, if the/products. aspx webpage. the logo.jpg file in the aspx webpage has a relative reference, but it is referenced by/products/books. when the browser displays a webpage, it will send a request to/products/logo.jpg instead of/logo.jpg. To reference this file correctly, make sure that you use the root directory to restrict (root qualify) CSS and image reference ("/style.css" instead of “style.css "). For ASP. NET controls, you can also use "~" Syntax reference a file from the root directory of your application (for example, <asp: image imageurl = "~ /Images/logo.jpg "runat =" server "/> ).

I hope this article will help you with ASP. NET and URL rewriting.

  1. Brief Introduction to ASP applications
  2. IScriptControl of ASP. NET
  3. ASP. NET Authentication Service
  4. Overview ASP. NET Security
  5. ASP. NET ISAPI

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.