Using URL rewriting in ASP.

Source: Internet
Author: User

The URL rewriting middleware has been added to ASP. NET Core 1.1 Preview 1, which finally allows for URL rewriting and a real-life experience.

The first thing to do is to upgrade the ASP. NET core Project to 1.1 Preview 1 (reference. NET Cross-platform tour: Upgrade the sample site to. NET Core 1.1 Preview 1), then add a reference to "Microsoft.AspNetCore.Rewrite" in Project.json and run the donet restore installation corresponding n Uget package.

"Dependencies": {    "Microsoft.AspNetCore.Rewrite": "1.0.0-preview1-final"}

Then, in Startup.cs's Configure () method, pass the app. Userewriter () Configures rewrite rules, such as to rewrite http://about.cnblogs.com/as Http://about.cnblogs.com/about/intro, so to write:

 Public void Configure (Iapplicationbuilder app, Iloggerfactory loggerfactory, ihostingenvironment env) {    app. Userewriter (new rewriteoptions (). Addrewrite ("^$"""about/intro"true));}

The 1th parameter of Addrewrite ("^$") is a regular expression that matches the URL before the rewrite, and the 2nd parameter ("About/intro") is the rewritten URL, and the 3rd parameter (true) indicates whether to discard subsequent rule matches after a successful 1 rule.

For URL matching, or Microsoft's usual, the beginning of the URL without a slash, I personally prefer to start with a slash:

// Unsupported notation app. Userewriter (new rewriteoptions (). Addrewrite ("^/$"""/about/intro"true));

Be aware of the app. Userewriter () to be placed in the app. Usemvc (), app. Usemvcwithdefaultroute (), app. Userouter () before.

Using URL rewriting in ASP.

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.