ASP. NET page URL rewrite

Source: Internet
Author: User
Tags httpcontext

without change, the page path is index.aspx?id=1, and now when you enter the page path INDEX/1, you can also access the page, a process called URL rewriting①: Create a path rewrite rule in a class, the following is a custom Urlrewriterfilter class, you need to inherit the interface IHttpModule②: Configuration in the configuration file (if the custom class is written in another class library, you need to change the. dll file generation path for that class library to match the startup item. dll file path) In addition, the URL rewrite of ASP can be configured directly when the website is published, and IIS chooses URL rewriting function .The first step:
usingSystem;usingSystem. Collections. Generic;usingSystem. Linq;usingSystem. Text;usingSystem. Threading. Tasks;namespacehttpmodule{usingSystem.    Web; usingSystem. Text.    RegularExpressions;  Public classUrlrewriterfilter:ihttpmodule { Public voidDispose () {Throw Newnotimplementedexception (); }        /// <summary>        ///responsible for registering a rewrite INDEX/1 on the first pipeline event URL for index.aspx?id=1/// </summary>        /// <param name= "context" ></param>         Public voidInit (HttpApplication context) {context. BeginRequest+=context_beginrequest; }        voidContext_beginrequest (Objectsender, EventArgs e) {            //01. Get the original URL of the current request INDEX/1            stringURL =HttpContext. Current. Request.            RAWURL; //02. Rewrite the current URL//define a regular expression to check if the currently sent URL is the index page path I want to overrideRegex reg =NewRegex ("/index/(. *)" ); if(reg. IsMatch (URL)) {stringNewurl = Reg. Replace (URL,"/index.aspx?id=$1" ); HttpContext. Current.            RewritePath (Newurl); }        }    }}

Step Two:

  < system.webserver >    < modules >      < add name ="" type =" /c9>"/>    </modules >  </system.webserver >

ASP. NET page URL rewrite

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.