Let's talk about modifying the form's action attribute when using urlrewrite.

Source: Internet
Author: User
Although I generally belong to people who don't write anything, I was more than a year away from the previous post, so I had to feel the time was so horrible ......

Recently, I started my business and launched a web 2.0 project. Previously, the Ajax and URL rewriting technologies, as well as the so-called XHTML + CSS + Div, were only in the theoretical stage and now I have the opportunity to put them into practice, as a result, when you play urlrewrite, you will encounter ASP. net httpform automatically assigns the real page address to the action attribute.

After a Google attack on the Internet, there are three solutions: add the client Java Script and reload the httpform or page class.

This post explains how to add client scripts and load httpform class: http://www.cnblogs.com/JeffreyZhao/archive/2006/12/27/604373.html

Magio ...... Next time I rename it "dad", hey) This explains how to reload the page: http://www.cnblogs.com/kaima/archive/2006/12/27/604758.html

Urlrewrite itself can hide the server's processing details. It is very awkward to hand this part over to the client. Therefore, the Java Script scheme will soon be rejected by me.

I have also rejected the heavy load httpform class, which can be viewed by reflector.CodeThe renderatttributes method of httpform also contains code for handling the onsubmit event of the client. A considerable number of Web controls depend on this part of the function, and the framework structure will be damaged after being removed. Msdn taught everyone to use this method, and it is still something that needs to be doubted.

The method described by Ma Ge is reasonable, but I think that reload of the page class should also be avoided under normal circumstances. One is to decide which page to use the new page class and which page does not need to be troublesome, to save trouble. you can also set the pagebasetype attribute in config, but this class does not meet the detailed requirements of the entrepreneurial website even if the page of the entire website needs to pass through.

Is there a better way? There is actually something I realized today when studying ASP. NET CSS friendly adapters.

This solution was modified based on Mago's solution above, provided that. NET 2.0 is supported.

. NET 2.0 Framework to ASP. net has added several special directories, one of which is the most easily overlooked app_browsers Directory, which is used to store browser definition files, the relevant instructions can refer to msdn: http://msdn2.microsoft.com/zh-cn/library/ms228122 (vs.80 ). aspx

Create the app_browsers directory in the root directory of the website, and create a new file named rewriteform. browser. The content is as follows:

< Browsers >
< Browser RefID = "Default" >
< Controladapters >
< Adapter Controltype = "System. Web. UI. htmlcontrols. htmlform"
Adaptertype = "Kuang. htmlformadapter"   />
</ Controladapters >
</ Browser >
</ Browsers >

In the browser section, the refID = "default" attribute indicates the original default of the extended system. browser file (in % WinDir % \ Microsoft. net \ framework \ v2.0.50727 \ config \ browsers), default. browser is the root defined by all browsers. For details, refer to the msdn instructions.

Adapter refers to the adapter, which in the. NET field represents the objects that are coordinated between two objects, such asThe sqldataadapter class is the adapter that coordinates between sqlcommand and dataset.

ASP. NET 2.0 introduces the concept of controladapter, meaning that Rewriteform. in the Browser file, use <adapter controltype = "system. web. UI. htmlcontrols. htmlform "adaptertype =" Kuang. htmlformadapter "/> in this line, I have specified the htmlform class to be reloaded and provided Kuang of the custom controladapter class. htmlformadapter.

The following is the custom controladapter class code:

Using System;
Using System. Web. UI. adapters;

Namespace Kuang {
Public   Class Htmlformadapter: controladapter {
Protected   Override   Void Render (system. Web. UI. htmltextwriter writer ){
Base . Render ( New Formrewritetextwriter (writer ));
}
}
}

Like the method of reloading the page class, this code also references a custom htmltextwriter class. The following is the implementation code of this class and I have made some modifications myself:

Using System;
Using System. IO;
Using System. Web;
Using System. Web. UI;

Namespace Kuang {
Public   Class Formrewritetextwriter: htmltextwriter {
Public Formrewritetextwriter (textwriter writer ): Base (Writer ){
If (Writer Is Htmltextwriter)
This . Innerwriter = (Writer As Htmltextwriter). innerwriter;
Else
This . Innerwriter = Writer;
}

Public   Override   Void Writeattribute ( String Name, String Value, Bool Fencode ){
Httpcontext Context = Httpcontext. Current;
Object Rewrotealready = Context. items [ " Formactionrewrotealready " ];
If (Name =   " Action "   && Rewrotealready =   Null ){
Value = Context. Request. rawurl;
Context. items [ " Formactionrewrotealready " ] =   New   Object ();
}
Base . Writeattribute (name, value, fencode );
}
}
}

Put the above two pieces of code under the app_code directory of the website. This method has a special advantage that you do not need to modify any code of the original website to connect to the Web. you do not need to change the configurations.

You can try it out.

BTW: As mentioned by Lao Zhao, the coexistence of updatepanel and form cannot be submitted for the second time. I have never encountered any problems, is it because my updatepanel is embedded in form instead of updatepanel?

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.