2

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Sean
  • 2,531
  • 2
  • 17
  • 17
  • The article you refer to was written in 2007. ASP.NET Core should never require such any more. You should be able to use its own URL routing. – Lex Li Feb 14 '18 at 15:17
  • Updated my question – Sean Feb 14 '18 at 17:10
  • Read into the comments after the accepted answer, https://stackoverflow.com/questions/3991552/how-to-know-the-original-url-after-url-rewrite – Lex Li Feb 14 '18 at 19:59

1 Answers1

2

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

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • 1
    Your answer relates to an app's "internal" routing, which isn't the problem I'm trying to solve. I'm trying to have my domain name serve as the "app selector", so hopefully you can see that the app's internal routing table won't help which app is selected. I'm using IIS URL Rewrite to accomplish the act of a simple URL selecting the app with a long path rewrite, but once the URL is rewritten, the app itself still thinks it's at the longer URL so all the asp-page references use the longer URL. That's where Scott Gu's recommendation to use RawURL would be nice but that's not available in Core – Sean Feb 14 '18 at 22:30
  • This is *exactly* the problem routing is meant to solve. You can make a [custom `IRouter` to select the tenant based on domain name](https://blog.maartenballiauw.be/post/2015/02/17/domain-routing-and-resolving-current-tenant-with-aspnet-mvc-6-aspnet-5.html) (although that example is a bit outdated - there is no longer an `IsHandled` property - see [this answer](https://stackoverflow.com/a/48789831) for the fix to that). – NightOwl888 Feb 15 '18 at 04:29
  • Of course, this assumes that all of your "applications" are part of the same project ([Areas](https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas) would work nicely for this). If you have multiple applications, IIS and other web servers already allow you to configure separate domain names for each application - there is no reason to use rewriting in that case, either. – NightOwl888 Feb 15 '18 at 04:33
  • Ya, I think you're still missing my objective here. Let's recap. My goal is to select an app by domain alone. Option 1) .NET Routing, only works within an app, not helpful here. 2) Virtual directories, don't quite hit the mark, because the domain doesn't dictate the app, the name of the VD does. 3) IIS Rewrite rules, works, but screws up the app's page references. If you can elucidate a solution that works, I would be most grateful. – Sean Feb 17 '18 at 00:45
  • Also 4) IIS Bindings at the individual site level don't allow for multiple domains, but not even sure this works at all towards my goal – Sean Feb 17 '18 at 00:59
  • @scottgu, help? – Sean Feb 17 '18 at 01:01
  • Got it. I was able to piece together from your vague suggestions that IIS bindings were indeed the answer. I had to create a new web site, assign it the domain I wanted, and remove the * binding from the 'Default Web Site'. If you provide this detailed solution as a full answer to this question, I'll mark it as answered to your credit – Sean Feb 17 '18 at 02:37
  • Great. I've updated my answer to include IIS Setup. Sorry for being so vague - IIS going way back supported multiple web sites with individual domain name bindings (and application pools). Your [XY question](http://xyproblem.info/) threw me because most reach that conclusion first before they start looking at URL rewriting, since the complexity of the former is far less than the latter. Also, you mentioned a missing property in .NET Core, which implies your use case requires programming (it does not). – NightOwl888 Feb 17 '18 at 05:13
  • I like to start with the most complicated solutions first. Builds character :) – Sean Feb 17 '18 at 05:24