-->

.net core routing to a razor pages area based on c

2019-08-02 08:21发布

问题:

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.

回答1:

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.