After ASP. neturl is rewritten, there is processing for page sending back

Source: Internet
Author: User

Source: http://blog.csdn.net/pc01/archive/2006/06/23/824872.aspx

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 creating and compiling this class, you must first add it to the references folder of the Web application to use it in the ASP. NET web application. Then, use it to replace the htmlform class by adding the following content at the top of the ASP. NET webpage:

<% @ Register tagprefix = "SKM" namespace = "actionlessform"
Assembly = "actionlessform" %>
Then, replace <form runat = "server"> (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; // a false 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>

This method should be the simplest and do not need to change the original code.

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.