Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Core 2.0 URL Rewriting

0.00/5 (No votes)
1 Sep 2017 1  
How to implement URL rewriting in ASP.NET Core. Continue reading...

Problem

How to implement URL rewriting in ASP.NET Core.

Solution

In Startup, configure RewriteOptions and middleware for URL rewriting:

public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            var rewrite = new RewriteOptions()
                .AddRedirect("films", "movies")
                .AddRewrite("actors", "stars", true);

           app.UseRewriter(rewrite);

            app.Run(async context =>
            {
                var path = context.Request.Path;
                var query = context.Request.QueryString;
                await context.Response.WriteAsync($"New URL: {path}{query}");
            });
        }

Run and browse to /films, you’ll notice a redirect:

URL Rewriting - films

Browse to /actors, you’ll notice a rewrite:

URL Rewriting - actors

Discussion

URL Rewriting is about modifying the request URL based on custom rules in order to decouple the address from the underlying resource. This could be for reasons like security, SEO, user friendly URL and redirect HTTP to HTTPS etc.

When you’re unable to use the rewriting features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Rewrite middleware. It’s performance and feature set won’t match server based rewriting features though.

Rewrite middleware is capable of doing both redirects and rewrites:

Redirect

It’s a client-side operation and works like this:

  1. Client requests a resource e.g., /films
  2. Server responds with status code 301 (Moved Permanently) or 302 (Found) with new Location header, instructing client to request the new location e.g. /movies
  3. Client makes request to new location, which will also be shown in the address bar.

Rewrite

It’s a server-side operation and works like this:

  1. Client requests a resource e.g. /actors
  2. Server will internally map to new location e.g. /stars and return 200 (OK).
    1. Client will not know anything about this internal process and will still see the requested URL in address bar.

Rules

Redirect and Rewrite rules are Regular Expressions, details of which could be found in Useful Links section below.

Custom Rules

You could also create custom rules by creating a class and implementing IRule interface:

public class MoviesRedirectRule : IRule
    {
        private readonly string[] matchPaths;
        private readonly PathString newPath;

        public MoviesRedirectRule(string[] matchPaths, string newPath)
        {
            this.matchPaths = matchPaths;
            this.newPath = new PathString(newPath);
        }

        public void ApplyRule(RewriteContext context)
        {
            var request = context.HttpContext.Request;

            // if already redirected, skip
            if (request.Path.StartsWithSegments(new PathString(this.newPath)))
            {
                return;
            }

            if (this.matchPaths.Contains(request.Path.Value))
            {
                var newLocation = $"{this.newPath}{request.QueryString}";

                var response = context.HttpContext.Response;
                response.StatusCode = StatusCodes.Status302Found;
                context.Result = RuleResult.EndResponse;
                response.Headers[HeaderNames.Location] = newLocation;
            }
        }
    }

Custom rule can be added to RewriteOptions:

var rewrite = new RewriteOptions()
                .Add(new MoviesRedirectRule(
                        matchPaths: new[] { "/films", "/features", "/albums" },
                        newPath: "/movies"));

Useful Link

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here