.net core routing to a razor pages area based on current domain name?

折月煮酒 提交于 2019-12-11 10:43:46

问题


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.



来源:https://stackoverflow.com/questions/52547068/net-core-routing-to-a-razor-pages-area-based-on-current-domain-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!