I need to separate controllers by ports inside netcore2.0 selfhosted web service.
Example:
There are 2 ports(p1 and p2) and 3 controllers(c1, c2, c3). Requirement scheme: c1 processes requests from p1, but c2 and c3 will processes requests from p2.
Any ideas about how can i do that?
Got answer on 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 ;
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With