asp.net url pseudo-Static rewrite implementation method _ Practical skills

Source: Internet
Author: User
Tags httpcontext
The web.config is set as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<!--overred-->
<configuration>
<configSections>
<section name= "Rewriterconfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/ >
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LOOKFOR>~/D (\d+) \.aspx</lookfor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<add verb= "*" path= "*.aspx" type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
</system.web>
</configuration>

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.
&LT;LOOKFOR&GT;~/D (\d+) \.aspx</lookfor>
<SendTo>~/default.aspx?id=$1</SendTo>
&LT;LOOKFOR&GT;~/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 digits, and ends with. aspx). Users can also set themselves according to their own needs.
<SendTo>~/default.aspx?id=$1</SendTo>
That represents how to override a URL 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 number in the file name requested by the user.
For example, the 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 has the effect of hiding the real filename and making it easier for the user to remember.
Handling postbacks
If a postback is generated in the rewritten URL, such as a button, and the overridden ASPX is invoked, the user's browser will display the actual address of the ASPX file, which is http://hostname/default.aspx?id=11. But from a user's perspective, if you click a button and suddenly see a URL change, it makes them uncomfortable. The problem must therefore be addressed.
There are two solutions:
(1) Define a Actionlessform class yourself and no longer use the system-supplied form tag in aspx
Copy Code code as follows:

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 create this class and compile it, you should first add it to the References folder of your Web application when you want to use it in an asp.net Web application. Then, to use it instead of the HtmlForm class, add the following at the top of the asp.net page:
<%@ 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 the:</skm:form>
/******************************************/
The above method: The page will appear create control item error situation, then introduce the above method
Delete the page's <%@ Register tagprefix= "SKM" namespace= "Actionlessform" assembly= "Actionlessform"%>, will: <skm:form id= " Form1 "method=" POST "runat=" Server >
(if any) Replace with <form runat= "Server" > and replace the </skm:form> tag on the right with:</form>
Then perform the following procedure
(2) The above is inherited form, the second way is to inherit page, so you do not need to change in the ASPX page anything.
Code:
Copy Code code as follows:

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 refer to it in your project.
Then rewrite the code for the inherited page class in the CS file for all ASPX files in the project to the inherited Olpage.
For example
public class Webform1:page
Rewritten as
public class Webform1:url. Olpage
This will solve the postback problem.
(3) Clear the form action through client code.
For the ASPX page, we find that when the client looks at the code, it automatically adds an action to the form, and the address is the original page address at the beginning. For an ASPX page, if the action is empty, it will be sent back to the current address. This allows us to continue to have the 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 method should be the simplest and will not 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.