区域和子域路由 [英] Area and subdomain routing

查看:84
本文介绍了区域和子域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET Core应用程序中创建了管理区域",并像这样更新了我的路线:

I created Admin Area inside my ASP.NET Core application and updated my routes like that:

app.UseMvc(routes =>
{
    routes.MapRoute(name: "areaRoute",
    template: "{area:exists}/{controller=Home}/{action=Index}");

    routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}");
});

我想在我的应用程序内部实现子域路由,以便在我们键入URL admin.mysite.com时呈现我的管理区域(mysite.com/admin).

I would like to implement subdomain routing inside my application in order to when we type URL admin.mysite.com, I render my Admin area (mysite.com/admin).

我看到了许多有关ASP.NET MVC 5的示例,但我无法适应ASP.NET Core.

I saw many examples for ASP.NET MVC 5, but I have not been able to adapt for ASP.NET Core.

推荐答案

我尝试了最后一个解决方案,但不适用于ASP.NET Core 1.1

I tried the last solution and did not work for ASP.NET Core 1.1

Microsoft有一个名为Rewrite的nuget软件包,这是一个中间件,您可以在其中重写或重定向某些请求,还可以编写自定义Rule的方法,在其中可以捕获子域并将其添加到请求路径:

Microsoft has a nuget package named Rewrite, A middleware where you can rewrite or redirect some requests, but also there is a way to write a custom Rule, where you can capture the subdomain and add it to the request path:

public class RewriteSubdomainRule : IRule
    {
        public void ApplyRule(RewriteContext context)
        {
            var request = context.HttpContext.Request;
            var host = request.Host.Host;
            // Check if the host is subdomain.domain.com or subdomain.localhost for debugging
            if (Regex.IsMatch(host, @"^[A-Za-z\d]+\.(?:[A-Za-z\d]+\.[A-Za-z\d]+|localhost)$"))
            {
                string subdomain = host.Split('.')[0];
                //modifying the request path to let the routing catch the subdomain
                context.HttpContext.Request.Path = "/subdomain/" + subdomain + context.HttpContext.Request.Path;
                context.Result = RuleResult.ContinueRules;
                return;
            }
            context.Result = RuleResult.ContinueRules;
            return;
        }
    }

在Startup.cs上

On Startup.cs

您必须添加中间件才能使用自定义重写规则:

You have to add the middleware to use the custom rewrite rule:

 app.UseRewriter(new RewriteOptions().Add(new RewriteSubdomainRule())); 

在这行之后,我定义了一条路由,该路由接收添加在请求路径上的子域并将其分配给子域变量:

And after this lines I define a route that receives the subdomain added on the request path and assign it to the subdomain variable:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapRoute(
            name: "subdomain",
            template: "subdomain/{subdomain}/{controller=Home}/{action=Index}/{id?}");
    });

在控制器上,您可以像这样使用它

On the controller you can use it like this

public async Task<IActionResult> Index(int? id, string subdomain)
{
}

这篇关于区域和子域路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆