Area and subdomain routing

前端 未结 5 821
遇见更好的自我
遇见更好的自我 2021-01-06 09:34

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

app.UseMvc(routes =>
{
    routes.MapRoute(name: \"areaRoute\",
          


        
5条回答
  •  逝去的感伤
    2021-01-06 09:55

    Michael Graf has a blog post about it.

    Basicly you need a custom router:

    public class AreaRouter : MvcRouteHandler, IRouter
    {
        public new async Task RouteAsync(RouteContext context)
        {
            string url = context.HttpContext.Request.Headers["HOST"]; 
            var splittedUrl = url.Split('.');
    
            if (splittedUrl.Length > 0 && splittedUrl[0] == "admin")
            {
                context.RouteData.Values.Add("area", "Admin");
            }
    
            await base.RouteAsync(context);
        }
    }
    

    And then register it.

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

    On the other hand we have the IIS Rewrite module, or even a Middleware

提交回复
热议问题