This video discusses ASP.NET Core middleware, and then covers the new URL rewriting middleware component.
- [Narrator] Before covering the three new features in 1.1 related to middleware, it's worth discussing middleware briefly. So middleware components handle the requests and the responses. Each component chooses to send the request to the next component, or not, and can perform work before, and after invoking the next component. In ASP.NET Core, middleware components replace HTTP modules and HTTP handlers, and they're completely configured in code.
The order of processing is dependent on the order that it is inserted into the pipeline. So if you put things in the wrong order, you could shoot yourself in the foot. Now the order for responses is reversed of the order of requests, and it only makes sense. The first new middleware feature in 1.1 that I'm going to talk about is URL Rewriting Middleware. And as one would expect, it modifies a requested URL based on a set of rules.
It requires the Microsoft.AspNetCore.Rewrite package, and it's capable of redirects, which happen client-side, as well as rewrites, which happen server-side. It could be configured in many ways. Of straight-up text, usually using a regular expression. Now it is important to note that the next is case-sensitive. You can also use IIS standard XML formatted rules, or Apache Mod_Rewrite syntax, although I'm also going to point out here that not all of the IIS rules are supported.
And you can use straight-up C# code with methods, or very special classes. There is a change in 2.0, and it's how the configuration of the IIS and Apache rules are done, and there's also additional IIS rules supported in 2.0. So a redirect is configured with a regular expression, then the replacement, and a status code. If you don't add in a status code, it defaults to 302, which is temporary, but you could also supply, for example, a 301.
You can also set it to redirect to HTTPS. Rewrites are also configured with regular expressions and a replacement value, and then an option of whether or not to continue processing rules. You can create specially-formatted methods to change the response, obviously through code, and you can also add classes that implement the IRule interface. I don't cover the IRule classes in this class, but you can find more information on how to do that at the URL on your screen.
So let's look at how this all works in code. Now I am in the configure method of the startup class. And we start the URL rewriting process by setting some options. And so on line 167, I declare a new instance of rewrite options, and then I add a redirect. Again, redirects are client-side. So anybody who types in 'foo' will get redirected to Products/Featured. And I've added in a 302, which I didn't have to because it defaults to 302, but I wanted to show where the status code goes.
You can also use a regular expression. So if somebody starts off with products, and types the next character after slashes anything but an F, a P or a number, it'll get redirected to products/featured. So the dollar sign one represents that first grouping in the regular expression. The rewrite is server-side, so if they type in bar it'll redirect them to products/featured, but it will not change the URL in the browser, and I'm also skipping any remaining rules.
I also have a method here, RedirectXmlRequests, which we will step through shortly and see how that code works. Once we have our options configured, we just say app.UseRewriter, passing in those options as I'm doing here on line 175. So here I have Firefox up with the developer tools loaded. And let's type in '/FOO' but we're going to do all caps, and we see we get a 404 because it couldn't find it.
Now I'm actually going to switch browsers here, because if I put in the right URL, so if I put in, this is just an interesting annoyance, if you will, I can type 'foo' in here, it'll actually work, because I've already navigated to lowercase 'foo.' So you saw the browser switch it to lowercase 'foo' and then redirect it to Products/Featured. Let me do that again, but turn on the developer tools. And I'm going to go back and type in 'foo,' and on the network tab we see that the name 'foo' has a status of 302, but it's actually running the featured page.
So then if I do Products/r, for example, it also redirects with a 302 to the featured page. But if I just type in 'bar,' it comes back with a 200 and a redirect doesn't change my URL, but it is rendering the Products/Featured page. So let's look at how the XML rewrite rules work.
And what we'll do is we'll go down to that method, and we'll put a break point. Now, an interesting tidbit is every URL is going to get processed by all of your rules. So the more rules you have, the higher potential you have for slowing down your site. So I'm switching back to Firefox to avoid any caching issues, and let me just type in SampleXMLFile.xml.
And I'm going to hit enter. We see here we go into the redirect request code, and it gets a rewrite context. So from that context are the requests. We're checking the path to make sure that we aren't already in the XML files directory. If we are, then we don't have to do anything different. Otherwise, we check to see if the requested path ends with .xml, and we're ignoring case differences.
And we're going to change the status code to a 301 moved permanently, and we're going to change the location to /xml files, and then build up the rest of the string. So we are just redirecting them regardless of how they came into our site, or what URL. If they're asking for an XML file, we're pointing them into this specific directory. And we continue running, and we see our XML file is rendered. And just to show you, I have my sample XML file right here, under the XML files directory of wwwroot, and there is the XML.
So let's stop running this, and go back into our startup. A couple more things I want to show you that I'm not actually going to walk through. If I wanted to redirect to HTTPS, I can just add the 'add redirect to HTTPS' status code and port. Here's how we would add ApacheMod_Rewrite, and we have to have that ApacheMod_Rewrite.txt file. Same thing with the IIS, where we have the XML file passed in, and that just builds up the options.
As I mentioned, I don't have an example showing the IRule interface. It's a lot of code, but it is very well spelled-out in the documentation.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages