Implement URL rewrite under. NET, static page

Source: Internet
Author: User
Tags add bool config httpcontext implement net regular expression string
URL rewriting is the process of intercepting incoming WEB requests and automatically redirecting requests to other URLs. For example, the browser sent to request hostname/101.aspx, the server automatically directed this request to http://hostname/list.aspx? id=101.

One what is URL rewriting
URL rewriting is the process of intercepting incoming WEB requests and automatically redirecting requests to other URLs. For example, the browser sent to request hostname/101.aspx, the server automatically directed this request to http://hostname/list.aspx? id=101.
The advantage of URL rewriting is that:
L shorten the URL, hide the actual path to improve security
• Easy user memory and typing.
L easy to be indexed by search engines

Two basic methods of implementing URL rewriting
1. Download the URLRewriter.dll of MS and put it under the bin of your Web program
Download Address 1:http://www.sinoec.cn/fordown/urlrewriter.dll
Download Address 2:
Download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/msdnurlrewriting.msi

After the download is complete, set the following in Web.config:

<?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 "/>
where
<section name=" Rewriterconfig "
type=" URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter "/>
</ Configsections>
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

configuration section "Rewriterconfig" reads as follows
<rewriterconfig>
<rules>
<rewriterrule>
<lookfor >~/D (\d+) \.aspx</lookfor>
<sendto>~/default.aspx?id=$1</sendto>
</RewriterRule
</rules>
</rewriterconfig>
Where the key is these two sentences
<lookfor>~/d (\d+) \.aspx</lookfor
<sendto>~/default.aspx?id=$1</sendto>
<lookfor>~/d (\d+) \.aspx</lookfor> says 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>, which indicates how the URL is overwritten 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.
such as 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.


2. Process the postback
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

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 the <form runat= "Server" > (if any) with the following:
<skm:form id= "Form1" method= "POST" runat= "Server" >
and replace the </form> tag on the right with the following:
</skm:Form>

(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:

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.



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.