Hosting multiple product APIs in single ASP.NET Core WebAPI Service

Sathish Guru V

I am designing an ASP.NET Core based Web API, which needs to support multiple variants of my product, let's say based on a license or the variety which it was installed.

Instead of going for multiple services for each type of product, I thought of a single service which houses/hosts multiple Endpoints or URLs. I will make this configurable in the appsettings.json at the time of installation.

I am aware of the UseUrls on creating the WebHost, but can I bind the specific URL in a set of URLs to specific Controllers?

Code:

WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5000;http://localhost:5001;https://localhost:5002")

Expect

https://localhost:5000/ --> Product1Controller
https://localhost:5001/ --> Product2Controller
https://localhost:5002/ --> Product2Controller

I am new to the ASP.NET Core, please help me if this is achievable or not. Thanks in advance.

Ryan

A workaround is to add custom Constraint on api controller which is enabled with specific port.

1.Create a PortActionConstraint class:

[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.Add attribute with correspond port number on all controller like

[PortActionConstraint(5000)]
[Route("api/[controller]")]
[ApiController]
public class Product1Controller : ControllerBase

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Hosting Web API in .Net Core Worker Service - cannot reference IWebHostEnvironment

分類Dev

ASP.NET Core hosting environment variable ignored

分類Dev

ASP.NET Core hosting - 500 internal server error

分類Dev

Properly handling DbContexts in ASP.NET Core WebApi

分類Dev

SPA(Aurelia)+ ASP.NET Core WebAPI + Google認証

分類Dev

ASP.NET Core WebApiでのZipArchiveの使用

分類Dev

ASP.NET Core 2.0 WebApiエラー

分類Dev

ASp .Net Core WebAPIの複数のMemoryCache

分類Dev

ASP.Net Core WebAPIへのSwaggerの追加

分類Dev

Custom serialization of null values in Asp.net core WebApi

分類Dev

Is it possible to determine the ASP.NET Core hosting environment in an HTML helper method?

分類Dev

use existing login table with asp.net core identity service

分類Dev

Deploy ASP.NET Core 5 app to Azure App Service?

分類Dev

Deploy ASP.NET Core 5 app to Azure App Service?

分類Dev

Asp Net Core 3 Sample with 503 Service Unavailable

分類Dev

Simple service injection in ASP.Net Core does not call constructor

分類Dev

ASP.NET WebAPI output gets 40× slower when single byte is added to response

分類Dev

ASP.NET Retrieve single value from a cookie with multiple keys

分類Dev

How to create RESTful web service using ASP.NET WebAPI without Visual Studio?

分類Dev

OData v4.0 via ASP.NET WebAPI - Can Excel consume a service?

分類Dev

Asp.Net Core WebAPIからJPEG画像を返す

分類Dev

ASP.net Core WebAPIでCORSを有効にする方法

分類Dev

Asp.Net Core WebAPIからJPEG画像を返す

分類Dev

ASP.NET Core WebApi自動ヘルプページ

分類Dev

Asp.net Core WebAPI-現在のユーザーとWindows認証

分類Dev

ASP.NET Core WebApiでのポストストリーム

分類Dev

ASP.NET Core 2.0 WebAPI応答キャッシュ

分類Dev

How to host the basic ASP.NET Core 2 WebAPI application on IIS

分類Dev

Asp.Net WebApi Core 2.0IDとJWTBearerのCookieなし

Related 関連記事

  1. 1

    Hosting Web API in .Net Core Worker Service - cannot reference IWebHostEnvironment

  2. 2

    ASP.NET Core hosting environment variable ignored

  3. 3

    ASP.NET Core hosting - 500 internal server error

  4. 4

    Properly handling DbContexts in ASP.NET Core WebApi

  5. 5

    SPA(Aurelia)+ ASP.NET Core WebAPI + Google認証

  6. 6

    ASP.NET Core WebApiでのZipArchiveの使用

  7. 7

    ASP.NET Core 2.0 WebApiエラー

  8. 8

    ASp .Net Core WebAPIの複数のMemoryCache

  9. 9

    ASP.Net Core WebAPIへのSwaggerの追加

  10. 10

    Custom serialization of null values in Asp.net core WebApi

  11. 11

    Is it possible to determine the ASP.NET Core hosting environment in an HTML helper method?

  12. 12

    use existing login table with asp.net core identity service

  13. 13

    Deploy ASP.NET Core 5 app to Azure App Service?

  14. 14

    Deploy ASP.NET Core 5 app to Azure App Service?

  15. 15

    Asp Net Core 3 Sample with 503 Service Unavailable

  16. 16

    Simple service injection in ASP.Net Core does not call constructor

  17. 17

    ASP.NET WebAPI output gets 40× slower when single byte is added to response

  18. 18

    ASP.NET Retrieve single value from a cookie with multiple keys

  19. 19

    How to create RESTful web service using ASP.NET WebAPI without Visual Studio?

  20. 20

    OData v4.0 via ASP.NET WebAPI - Can Excel consume a service?

  21. 21

    Asp.Net Core WebAPIからJPEG画像を返す

  22. 22

    ASP.net Core WebAPIでCORSを有効にする方法

  23. 23

    Asp.Net Core WebAPIからJPEG画像を返す

  24. 24

    ASP.NET Core WebApi自動ヘルプページ

  25. 25

    Asp.net Core WebAPI-現在のユーザーとWindows認証

  26. 26

    ASP.NET Core WebApiでのポストストリーム

  27. 27

    ASP.NET Core 2.0 WebAPI応答キャッシュ

  28. 28

    How to host the basic ASP.NET Core 2 WebAPI application on IIS

  29. 29

    Asp.Net WebApi Core 2.0IDとJWTBearerのCookieなし

ホットタグ

アーカイブ