dotnet / dotnet/aspnetcore

UseUrls equivilant API for Http and Https ports

Open
#44,772 2 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-networking Needs: Design
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

https://github.com/dotnet/aspnetcore/pull/44194 added config support in the host for mapping HTTP_PORTS and HTTPS_PORTS to URLS. @davidfowl suggested also adding an API for this on the host, similar to UseUrls.

https://github.com/dotnet/aspnetcore/issues/43135#issuecomment-1264538090

## Proposed API

Initial theories, needs work:
```diff
namespace Microsoft.AspNetCore.Hosting;

public static class HostingAbstractionsWebHostBuilderExtensions
{
+ public static IWebHostBuilder UseHttpPorts(this IWebHostBuilder hostBuilder, params int[] httpPorts);
+ public static IWebHostBuilder UseHttpsPorts(this IWebHostBuilder hostBuilder, params int[] httpsPorts);
+ public static IWebHostBuilder UsePort(this IWebHostBuilder hostBuilder, int port, bool useHttps);
+ public static IWebHostBuilder UsePorts(this IWebHostBuilder hostBuilder, int httpPort, int httpsPort);
}
```

## Usage Examples

```C#
var port = int.Parse(Environment.GetEnvironmentVariable("PORT") ?? "3000");

var builder = WebApplication.CreateBuilder(args);

buider.WebHost.UsePort(port);

var app = builder.Build();

app.MapGet("/", () => "Hello World");

app.Run();
```

## Alternative Designs

## Risks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.