Sean
Sean

Reputation: 2667

IIS URL Rewrite and .NET Core

My goal is to utilize IIS URL Rewrite to simplify my URL paths visible to the user, but without breaking my Core asp-page references

Example:. If my IIS rewrites sentoso.com/apple to sentoso.com/applications/app1/fruits/apple, how do I find the value sentoso.com/apple inside the asp app?

The internal rewrite feature of asp only applies to the routing after the app location, not the path to the app itself, which is why I need IIS rewrite to dig down to the app's directory. There is nothing asp rewrite can do to rewrite the example in the prior paragraph.

Scott Guy managed some RawUrl trickery in older asp that is shown here:

https://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net

But the RawUrl property doesn't exist in .NET Core.

Upvotes: 2

Views: 3886

Answers (1)

NightOwl888
NightOwl888

Reputation: 56909

Multiple Web Sites with Individual Domains

If you want to host multiple individual web sites in IIS, you just need to Create a Web Site for each web site, then Add a Binding for the domain name. IIS supports this out of the box, so there is no need for URL rewriting in this case.

A Multi-tenant Web Site with More than One Domain Name

In this case, using .NET Routing is far preferable to URL Rewriting.

URL Rewriting is effectively dead in .NET Core. It was needed in early versions of ASP.NET because the .aspx, .ashx, and .asmx handlers were file-based and if you wanted the URL to be different than the file name, you had to rewrite the incoming request to the file name. This was just a way to "fake" URLs by plugging different values into the HTTP request than the real values.

Rewriting caused several issues though, especially for components that relied on those HTTP request values to accurately represent the request.

However, years ago ASP.NET Routing was created. No more faking the HTTP Context - routing goes directly to the resource that is requested without rewriting and without redirecting. Even better: routing manages all of the URL activity for the entire application. Not only will it send an incoming URL to an exact page or action method, it can also be used to generate URLs to be used in hyperlinks, JavaScript, for redirects, etc. What this effectively means is that if you change the URL of a route, it will automatically update everywhere in the application.

Another difference is that routing doesn't map a URL to another URL, in MVC it maps a URL to a dictionary of route values. For example, you can map the URL apple to controller="Fruits", action="Apple". MVC will then respond by executing the FruitsController.Apple action method when you go to http://www.somesite.com/apple.

So, for all modern apps use .NET Routing instead of URL rewriting.

Reference: Routing in ASP.NET Core

Upvotes: 2

Related Questions