我需要通过netcore2.0自托管Web服务内的端口分隔控制器。
示例:
有2个端口(p1和p2)和3个控制器(c1,c2,c3)。 需求方案:c1处理来自p1的请求,但是c2和c3将处理来自p2的请求。
关于我该怎么做的任何想法?
答案 0 :(得分:-1)
在GitHub https://github.com/aspnet/Mvc/issues/8502上获得答案
[PortActionConstraint(5000)]
public class HomeController : Controller
{
...
}
[AttributeUsage(AttributeTargets.Class)]
public class PortActionConstraint : ActionMethodSelectorAttribute
{
public PortActionConstraint(int port)
{
Port = port;
}
public int Port { get; }
public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
{
//external port
var externalPort = routeContext.HttpContext.Request.Host.Port;
//local port
var localPort = routeContext.HttpContext.Connection.LocalPort;
//write here your custom logic. for example
return Port == localPort ;
}
}