Implementation method for pseudo-static rewrite of ASP
First of all, the ASP. NET URL pseudo-static just point ~/a_1.html to the ~/a.aspx?id=1, but a.aspx still exist, you don't have to./a_1.html to access, with./aaa.aspx?id=1 is also accessible, Pseudo-static and will really turn your a.aspx into a.html
Set the following in Web. config:
which
<section name= "Rewriterconfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/ >
The name of the handler class used to specify the configuration section "Rewriterconfig" is "URLRewriter.Config.RewriterConfigSerializerSectionHandler", This class exists in the Urlrewriter. dll file in the bin directory
The key is these two sentences.
<LOOKFOR>~/D (\d+) \.aspx</lookfor>
<SendTo>~/default.aspx?id=$1</SendTo>
<LOOKFOR>~/D (\d+) \.aspx</lookfor>
Indicates that the user-entered Url,d (\d+) \.aspx is a regular expression that matches the file name in the URL (where the letter D begins, followed by one or more numbers, and ends with. aspx). Users can also set their own according to their own needs).
<SendTo>~/default.aspx?id=$1</SendTo>
That indicates how the URL is rewritten when the server receives a request that meets the above criteria. This means accessing the defalutl.aspx and passing in the parameter ID, whose value will be represented by the first digit in the file name requested by the user.
For example, user input hostname/d11.aspx, the server will rewrite him as http://hostname/default.aspx?id=11. In other words, the user enters http://hostname/d11.aspx, the actual access is http://hostname/default.aspx?id=11. This will play a role in hiding the real file name and making it easy for users to remember.
Handling postbacks
In the rewritten URL, if a postback is generated, such as a button, and the overridden ASPX is called, the actual address of the ASPX file is displayed in the user's browser, which is http://hostname/default.aspx?id=11. But from the user's perspective, if you click the button and suddenly see the URL change will make them uncomfortable. The problem must therefore be addressed.
There are two ways to solve this problem:
(1) Define a Actionlessform class yourself and no longer use the system-supplied form tag 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 you have created and compiled this class, to use it in an ASP. NET Web application, you should first add it to the Web application's References folder. Then, to use it instead of the HtmlForm class, add the following to the top of the asp:
<%@ Register tagprefix= "SKM" namespace= "Actionlessform" Assembly = "Actionlessform"%>
Then replace <form runat= "Server" > (if any) with: <skm:form id= "Form1" method= "POST" runat= Server >
and replace the </form> tag on the right with:</skm:form>
/******************************************/
The above method: The page will appear to create a control error condition, the next introduction does not use the above method
the page <%@ Register tagprefix= "SKM" namespace= "Actionlessform" assembly= " Actionlessform "%> Delete, will: <skm:form id=" Form1 "method=" POST "runat=" Server ">
(if any) replaced with <form runat=" Server > Replace the </skm:form> tag on the right with:</form>
and then perform the following procedure
(2) above is inheriting a form, the second method is to inherit the page, This way you don't need to change anything in the ASPX page.
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;//False URL internal Form Fixerhtml32textwriter (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 Formfixerhtmltex Twriter (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.
The code for inheriting the page class in the CS file corresponding to all the ASPX files in the project is then overwritten with the inheritance olpage.
For example
public class Webform1:page
Rewritten as
public class Webform1:url. Olpage
This resolves the postback issue.
(3) Clear the action of the form through the client code.
For ASPX pages, when we look at the code on the client, we find that it automatically adds an action to the form, and that the address is the original page address at the beginning. In the case of an ASPX page, if the action is empty, it will be sent back to the current address. This allows us to maintain an address after the postback by clearing the action on the client.
Add the following code to the page:
<script type= "Text/javascript" >try{document.forms[0].action= ""}catch (ex) {}</script>
This pseudo-static rewrite method does not have to change the original code, it should be the most simple to use.
Implementation method for pseudo-static rewrite of ASP