10

In asp.net web api it was possible to map a route with a specific port to a controller like so:

public void Run1()
{
    HttpConfiguration config = new HttpConfiguration();
    ...
    config.Routes.MapHttpRoute(
       name: "Controller1",
       routeTemplate: "{action}",
       defaults: new { controller = "Controller1" }
    );

    Microsoft.Owin.Hosting.WebApp.Start("http://localhost:9000/base1/", config);
}

public void Run2()
{
    HttpConfiguration config = new HttpConfiguration();
    ...
    config.Routes.MapHttpRoute(
       name: "Controller2",
       routeTemplate: "{action}",
       defaults: new { controller = "Controller3" }
    );

    Microsoft.Owin.Hosting.WebApp.Start("http://localhost:9000/base2/", config);
}

public void Run3()
{
    HttpConfiguration config = new HttpConfiguration();
    ...
    config.Routes.MapHttpRoute(
       name: "Controller3",
       routeTemplate: "{action}",
       defaults: new { controller = "Controller3" }
    );

    Microsoft.Owin.Hosting.WebApp.Start("http://localhost:9050/base3/", config);
}

Run1();
Run2();
Run3();

This gives three distinct routes:

http://localhost:9000/base1/SomeAction -> SomeAction on Controller1
http://localhost:9000/base2/SomeAction -> SomeAction on Controller2
http://localhost:9050/base3/SomeAction -> SomeAction on Controller3

How can this be done with Kestrel in asp.net core?

I know, that I can specify the ports Kestrel listens to with IWebHostBuilder.UseUrls and the base path with IApplicationBuilder.UsePathBase. But how can I map a route to a specific port?

In addition: It seems that Kestrel (in opposite to Katana) could not be run on the same port more than once (see Run1 and Run2). Can somebody confirm that?

Jeldrik
  • 1,377
  • 1
  • 10
  • 35
  • 1
    Kestrel bind the port from the os, so it's blocked if you try to start a second listener it tries to bind the port again but as it is in blocked state Kestrel cant use it in a new Kestrel context – Fabian Kamp Nov 17 '21 at 17:15

0 Answers0