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.