ASP. net url Rewrite. URL rewriting

Source: Internet
Author: User

Url rewriting is the process of intercepting incoming web requests and automatically redirecting requests to other URLs.
For example, if the browser sends a request for hostname/101. asp tutorial x, the server automatically directs the request to http: // hostname/list. aspx? Id = 101.

Url rewriting has the following advantages:
Shorten the url and hide the actual path to improve security
Easy for users to remember and type.
Easy to be indexed by search engines

Ii. Basic Method for url rewriting
Download the ms urlrewriter. dll and put it in the bin of your web application.
1: http://www.rickel.cn/uploads/devtools/msdnurlrewriting.msi
2: bytes

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>
<Httphandlers>
<Add verb = "*" path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
</System. web>
</Configuration>


Where
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>

The handler class used to specify the configuration section "rewriterconfig" is named "urlrewriter. config. rewriterconfigserializersectionhandler", which exists in the urlrewriter. dll file in the bin directory.

The key is the two sentences.
<Lookfor> ~ /D (d +). aspx </lookfor>
<Sendto> ~ /Default. aspx? Id = $1 </sendto>
<Lookfor> ~ /D (d + ). aspx </lookfor> indicates the url entered by the user, d (d + ). aspx is a regular expression that matches the file name in the url (it starts with the letter d, followed by one or more numbers, and starts. the end of aspx. You can also set it based on your own needs ).
<Sendto> ~ /Default. aspx? Id = $1 </sendto> indicates how to rewrite the url when the server receives a request that meets the preceding conditions. Here, defalutl. aspx is accessed and the parameter id is passed in. The value $1 is represented by the first number in the file name of the user request.
For example, if you enter hostname/d11.aspx, the server will rewrite it to http: // hostname/default. aspx? Id = 11. In other words, when the user inputs http: // hostname/d11.aspx, the actual access is http: // hostname/default. aspx? Id = 11. In this way, you can hide the real file name and make it easier for you to remember.

Process sending back
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.html form
{
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 in the asp.net tutorial. 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) with: <skm: form id = "form1" method = "post" runat = "server">
Replace the </form> mark on the right with: </skm: form>

This method is not recommended for individuals.
(2) 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.html 32 textwriter)
{
Writer = new formfixerhtml32textwriter (writer. innerwriter );
}
Else
{
Writer = new formfixerhtmltextwriter (writer. innerwriter );
}
Base. render (writer );
}
}

Internal class formfixerhtml32textwriter: system.web.ui.html 32 textwriter
{
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.html textwriter
{
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

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.