2

I'm currently trying to architect a application to host multiple websites from a single .net core application.

Ideally, I would like to show completely different Razor pages based on the domain name the application is being accessed from.

Ideally, I would like to have my razor pages broken down into areas, where each area is associated with a different domain name. IE www.domain1.com is the domain1 area, www.domain2.com is the domain2 area, and each area has it's own index about page etc.

So if someone accesses the application from www.domain1.com/aboutus , it would direct them to the razor pages located in the Area/Domain1/Pages/AboutUs section of my application.

Similarly if my application were to receive a request to www.domain2.com/aboutus, they would see the about us page located under Area/Domain2/Pages/Aboutus.

I've been searching for hours, and I can't figure out how you accomplish this using razor pages area feature. The only information I can find is how to accomplish this using areas with vanilla MVC.

This was a pretty good example.

Different domain in the same app ASP.NET Core 2.0

I can't figure out how to accomplish the same type of thing using razor pages.

IMelancon
  • 21
  • 2
  • By area do you mean asp.net mvc area or just a subfolder? did you get chance to check https://aspnetboilerplate.com/ or https://www.cloudscribe.com/. Instead of starting from scratch, you can learn and adapt to what they already resolved. – Prashant Lakhlani Sep 28 '18 at 10:42
  • MVC areas, were added to razor pages in 2.1. https://www.mikesdotnetting.com/article/324/areas-in-razor-pages However, I cannot figure out how to use the domain name to constrain the route to specific areas. – IMelancon Sep 28 '18 at 13:13

1 Answers1

3

I'm not sure how scalable using Areas for this is, but you can use middleware to identify the domain and serve content from an area based on that, something like this, where "bloggs" is the name of an area for the bloggs.com domain:

public class AreaRoutingMiddleware
{
    private readonly RequestDelegate _next;

    public AreaRoutingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        if(context.Request.Host.ToString() == "bloggs.com")
        {
            context.Request.Path = "/bloggs" + context.Request.Path;
        }
        await _next.Invoke(context);
    }
}

You may also need to add checks for file types, unless all files (css, js, favicon etc) also reside in the respective area.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • I'm just having the exact same problem and the middleware works fine in debug mode but it doesn't work when deployed to my server. I got 2 domains pointing on the same server IP but somehow the Middleware always gets skipped. I can see in my server logs that the right request is coming in, so it's definitely my application that is altering the request string. – Elias Johannes Sep 03 '20 at 21:38