URL rewriting in. net

Source: Internet
Author: User
1. What is URL rewriting?
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. aspx, the server automatically directs the request to http: // hostname/list. aspx? Id = 101.
URL rewriting has the following advantages:
L Shorten the URL and hide the actual Path Improve Security
L Easy for users to remember and type.
L Easy to be indexed by search engines

Ii. Basic Method for URL rewriting
1. Download the MS urlrewriter. dll and put it on your webProgramUnder Bin
1: http://www.sinoec.cn/fordown/URLRewriter.dll
2:
Bytes

After the download is completeWeb. configThe settings are as follows:





Type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>




~ /D (\ D +) \. aspx
~ /Default. aspx? Id = $1





Type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>

Where

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 configuration section "rewriterconfig" contains the following content:



~ /D (\ D +) \. aspx
~ /Default. aspx? Id = $1


The key is the two sentences.
~ /D (\ D +) \. aspx
~ /Default. aspx? Id = $1
~ /D (\ D +) \. aspx 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 ).
~ /Default. aspx? Id = $1 Indicates how to rewrite the URL after 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 wayHide realityFile name to facilitate user memory.

2. 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. 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 creating and compiling this class, you must first add it to the references folder of the Web application to use it in the ASP. NET web application. Then, use it to replace the htmlform class by adding the following content at the top of the ASP. NET webpage:

Assembly = "actionlessform" %>
Then

(2) The above is to inherit a form, and 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
{< br> public class olpage: page
{< br> Public olpage ()
{}< br> protected override void render (htmltextwriter writer)
{< br> If (writer is system. web. UI. html32textwriter)
{< br> writer = new formfixerhtml32textwriter (writer. innerwriter);
}< br> else
{< br> writer = new formfixerhtmltextwriter (writer. innerwriter);
}< br> base. render (writer);
}

Internal class formfixerhtml32textwriter: system. Web. UI. html32textwriter
{
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. 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 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

In this way, the problem of sending back is solved.
Article from:Author overred
Reference announcement address:Http://www.yaosansi.com/trackback.asp? Tbid = 2

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.