Processing When a page is sent back after the URL is rewritten

Source: Internet
Author: User
If a sender is generated in the rewritten URL, for example, a button and the overwritten aspx is called, the actual address of the aspx file will be displayed in the user's browser, that is, http: // hostname/default. aspx? Id = 11. But from the user's point of view, if you suddenly see the URL change when you click the button, it will make them feel uneasy. Therefore, this problem must be solved.
There are two solutions:
(1) define an actionlessform class by yourself and no longer use the form tag provided by the system in aspx
 Namespace Actionlessform { Public   Class Form: system. Web. UI. htmlcontrols. htmlform { Protected   Override   Void Renderattributes (htmltextwriter writer) {writer. writeattribute ( "Name" , This . Name ); Base . Attributes. Remove ("Name" ); Writer. writeattribute ( "Method" , This . Method ); Base . Attributes. Remove ( "Method" ); This . Attributes. Render (writer ); Base . Attributes. Remove ( "Action" ); If ( Base . ID! = Null ) Writer. writeattribute ( "ID" , Base . Clientid );}}}

After this class is created and compiled, it must be applied to ASP. NET web applications.ProgramTo use it, you must first add it to the references folder of the Web application. Then, use it to replace the htmlform class by adding the following content at the top of the ASP. NET webpage:

 
<% @RegisterTagprefix ="SKM" Namespace ="Actionlessform"Assembly ="Actionlessform" %>

Then

 
<Form Runat ="Server">

Replace (if any):

<SKM: Form Id ="Form1" Method ="Post" Runat ="Server">

And replace the </form> mark on the right:
</SKM: Form>
(2) The above is to inherit a form, and the second method is to inherit the page, so you do not need to modify anything in the ASPX page.
Code:

 Using System; Using System. IO; Using System. Web; Using System. Web. UI; Namespace URL { Public   Class Olpage: Page { Public Olpage (){} Protected   Override   Void Render (htmltextwriter writer ){ If (Writer Is System. Web. UI. html32textwriter) {writer = New Formfixerhtml32textwriter (writer. innerwriter );} Else {Writer = New Formfixerhtmltextwriter (writer. innerwriter );} Base . Render (writer );}} Internal   Class Formfixerhtml32textwriter: system. Web. UI. html32textwriter {Private   String _ URL; // Fake URL          Internal Formfixerhtml32textwriter (textwriter writer ): Base (Writer) {_ url = httpcontext. Current. Request. rawurl ;} Public   Override   Void Writeattribute ( String Name, String Value, Bool Encode ){ If (_ URL! = Null && String . Compare (name,"Action" , True ) = 0) {value = _ URL ;} Base . Writeattribute (name, value, encode );}} Internal   Class Formfixerhtmltextwriter: system. Web. UI. htmltextwriter { Private   String _ URL; Internal Formfixerhtmltextwriter (textwriter writer ): Base (Writer) {_ url = httpcontext. Current. Request. rawurl ;} Public   Override   Void Writeattribute ( String Name,String Value, Bool Encode ){ If (_ URL! = Null && String . Compare (name, "Action" , True ) = 0) {value = _ URL ;} Base . Writeattribute (name, value, encode );}}}

Compile the file into a DLL and reference it in your project.

Then, rewrite the code of the inherited page class in the CS file corresponding to all aspx files in the project to inherit the olpage.
For example
Public class webform1: Page
Rewrite
Public class webform1: URL. olpage

(3) Clear the form action using the client code.

For the ASPX page, when we view the code on the client, we will find that it automatically adds an action to the form, and the address is the original page address mentioned at the beginning. For An ASPX page, if its action is empty, it will be sent back to the current address. In this way, you can clear the action on the client to ensure that the address remains available after sending the response.

Add the following code to the page:

 
<Script Type ="Text/JavaScript">Try{Document.Forms [0]. Action =""}Catch(Ex ){}</Script>
 
The HTML output is ordered. After the form is generated, the server. Transfer cannot pass the verification and the response. Redirect method is used.
 
   
   




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.