URL rewriting is simple. You only need to download a urlrewriter. dll file.

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 to 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 complete, set the following in Web. config:





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




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





type =" urlrewriter. rewriterfactoryhandler, urlrewriter "/>

Where
<Section name = "rewriterconfig"
Type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>
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:
<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /D (\ D +) \. aspx </lookfor>
<Sendto> ~ /Default. aspx? Id = $1 </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

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, the real file name is hidden and easy to remember.

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:

<% @ Register tagprefix = "SKM" namespace = "actionlessform"
Assembly = "actionlessform" %>
Then, replace <form runat = "server"> (if any):
<SKM: Form ID = "form1" method = "Post" runat = "server">
And replace the </form> mark on the right:
</SKM: Form>

(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
{< br> private string _ URL; // fake URL
internal formfixerhtml32textwriter (textwriter writer): Base (writer)
{< br> _ url = httpcontext. current. request. rawurl;
}< br> Public override void writeattribute (string name, string value, bool encode)
{< br> If (_ URL! = NULL & string. compare (name, "action", true) = 0)
{< br> value = _ URL;
}< br> base. writeattribute (name, value, encode);
}< BR >}< br> internal class formfixerhtmltextwriter: system. web. UI. htmltextwriter
{< br> private string _ URL;
internal formfixerhtmltextwriter (textwriter writer): Base (writer)
{< BR >_url = httpcontext. current. request. rawurl;
}< br> Public override void WR Iteattribute (string name, string value, bool encode)
{< br> If (_ URL! = NULL & string. compare (name, "action", true) = 0)
{< br> value = _ URL;
}< br> 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.
Full text

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.