更新时间:2023-02-15 08:15:30
一种解决方法是在启用了特定端口的api控制器上添加自定义约束。
A workaround is to add custom Constraint on api controller which is enabled with specific port.
1.创建 PortActionConstraint
类:
[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;
}
}
2。在所有控制器上添加具有相应端口号的属性像
2.Add attribute with correspond port number on all controller like
[PortActionConstraint(5000)]
[Route("api/[controller]")]
[ApiController]
public class Product1Controller : ControllerBase