Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

c# - Area and subdomain routing

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}");
});

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).

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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-zd]+.(?:[A-Za-zd]+.[A-Za-zd]+|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;
        }
    }

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)
{
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...