当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net Core MVC中怎么把二级域名绑定到特定的控制器上

Asp.net Core MVC中怎么把二级域名绑定到特定的控制器上

2017年12月08日  | 移动技术网IT编程  | 我要评论
应用场景:企业门户网站会根据内容不同,设置不同的板块,如新浪有体育,娱乐频道,等等。有的情况下需要给不同的板块设置不同的二级域名,如新浪体育sports.sina.com.

应用场景:企业门户网站会根据内容不同,设置不同的板块,如新浪有体育,娱乐频道,等等。有的情况下需要给不同的板块设置不同的二级域名,如新浪体育sports.sina.com.cn。

  在asp.net core mvc中,如果要实现板块的效果,可能会给不同的板块建立不同的控制器(当然也有其他的技术,这里不讨论实现方式的好坏),在这种情况下,如何给控制器绑定上独有的二级域名,比如体育频道对应的控制器叫sportcontroller,通过sports.xxx.com域名访问系统的时候,直接进入sportcontroller,并且通过这个二级域名无法访问其他的控制器。

  上面说完场景了,下面来看下如何实现。

  在asp.net core mvc中有路由规则配置,配置的地方在startup.configure方法中,具体代码如下:

app.usemvc(routes =>
{
   routes.maproute(
      name: "default",
      template: "{controller=home}/{action=index}/{id?}",
      defaults: new { area="admin"});
});

  遗憾的是不支持对域名的支持(我目前了解的是,如果有问题,欢迎大家指正)。通过routes.maprouter注册路由规则,并加入到routecollection中,当某个请求过来后,routercollection循环所有注册好的irouter对象,找到第一个匹配的irouter为止。虽然框架不支持域名配置规则,但是我们可以自己去实现一个irouter,在里面实现二级域名判断的逻辑,我这里暂时起名为subdomainrouter,具体实现代码如下:

public class subdomainrouter : routebase
 {
   private readonly irouter _target;
   private readonly string _subdomain;
   public subdomainrouter(
     irouter target,
     string subdomain,//当前路由规则绑定的二级域名
     string routetemplate,
     routevaluedictionary defaults,
     routevaluedictionary constrains,
     iinlineconstraintresolver inlineconstraintresolver)
     : base(routetemplate,
        subdomain,
        inlineconstraintresolver,
        defaults,
        constrains,
        new routevaluedictionary(null))
   {
     if (target == null)
     {
       throw new argumentnullexception(nameof(target));
     }
     if (subdomain == null)
     {
       throw new argumentnullexception(nameof(subdomain));
     }
     _subdomain = subdomain;
     _target = target;
   }
   public override task routeasync(routecontext context)
   {
     string domain = context.httpcontext.request.host.host;//获取当前请求域名,然后跟_subdomain比较,如果不想等,直接忽略
     if (string.isnullorempty(domain) || string.compare(_subdomain, domain) != 0)
     {
       return task.completedtask;
     }
      
     //如果域名匹配,再去验证访问路径是否匹配
     return base.routeasync(context);
   }
   protected override task onroutematched(routecontext context)
   {
     context.routedata.routers.add(_target);
     return _target.routeasync(context);
   }
   protected override virtualpathdata onvirtualpathgenerated(virtualpathcontext context)
   {
     return _target.getvirtualpath(context);
   }
 }

  从上面的代码我们只看到了域名检测,但是如何把域名定向到特定的控制器上,这就需要我们在注册这个irouter的时候做些文章,直接上代码:

public static class routebuilderextensions
  {    public static iroutebuilder mapdomainroute(
      this iroutebuilder routebuilder,string domain,string area,string controller)
    {
      if(string.isnullorempty(area)||string.isnullorempty(controller))
      {
        throw new argumentnullexception("area or controller can not be null");
      }
      var inlineconstraintresolver = routebuilder
        .serviceprovider
        .getrequiredservice<iinlineconstraintresolver>();
        string template = "";
          routevaluedictionary defaults = new routevaluedictionary();
          routevaluedictionary constrains = new routevaluedictionary();
          constrains.add("area", area);
          defaults.add("area", area);
          constrains.add("controller", controller);
          defaults.add("controller", string.isnullorempty(controller) ? "home" : controller);
          defaults.add("action", "index");
          template += "{action}/{id?}";//路径规则中不再包含控制器信息,但是上面通过constrains限定了查找时所要求的控制器名称
          routebuilder.routes.add(new subdomainrouter(routebuilder.defaulthandler, domain, template, defaults, constrains, inlineconstraintresolver));
      return routebuilder;
    }
}

  最后我们就可以在startup中注册对应的规则,如下:

public static class routebuilderextensions
  {    public static iroutebuilder mapdomainroute(
      this iroutebuilder routebuilder,string domain,string area,string controller)
    {
      if(string.isnullorempty(area)||string.isnullorempty(controller))
      {
        throw new argumentnullexception("area or controller can not be null");
      }
      var inlineconstraintresolver = routebuilder
        .serviceprovider
        .getrequiredservice<iinlineconstraintresolver>();
        string template = "";
          routevaluedictionary defaults = new routevaluedictionary();
          routevaluedictionary constrains = new routevaluedictionary();
          constrains.add("area", area);
          defaults.add("area", area);
          constrains.add("controller", controller);
          defaults.add("controller", string.isnullorempty(controller) ? "home" : controller);
          defaults.add("action", "index");
          template += "{action}/{id?}";//路径规则中不再包含控制器信息,但是上面通过constrains限定了查找时所要求的控制器名称
          routebuilder.routes.add(new subdomainrouter(routebuilder.defaulthandler, domain, template, defaults, constrains, inlineconstraintresolver));
      return routebuilder;
    }
}

以上所述是小编给大家介绍的asp.net core mvc中怎么把二级域名绑定到特定的控制器上,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网