翼度科技»论坛 编程开发 .net 查看内容

你知道如何修改ASP.NET Core默认端口吗?常用5种方法实例演示

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
下面针对 ASP.NET Core 中修改默认端口的五种常用方法的详细示例,分别对应 appsettings.json 配置 Kestrel 的 Endpoint、使用 UseUrls 方法、命令行参数方法、host.json 配置方法和使用 Docker 的方式。
方法一:appsettings.json 配置 Kestrel 的 Endpoint


  • 在 appsettings.json 中添加端口配置:
  1. {
  2.   "Kestrel": {
  3.     "EndPoints": {
  4.       "Http": {
  5.         "Url": "http://localhost:5001"
  6.       }
  7.     }
  8.   }
  9. }
复制代码

  • 在 Startup.cs 中读取配置:
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. public class Startup
  7. {
  8.     private readonly IConfiguration _configuration;
  9.     public Startup(IConfiguration configuration)
  10.     {
  11.         _configuration = configuration;
  12.     }
  13.     public void ConfigureServices(IServiceCollection services)
  14.     {
  15.         // 添加服务配置
  16.     }
  17.     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  18.     {
  19.         // 其他中间件配置
  20.         var url = _configuration["Kestrel:EndPoints:Http:Url"];
  21.         
  22.         // 使用 Kestrel Endpoint
  23.         app.Run(async (context) =>
  24.         {
  25.             await context.Response.WriteAsync($"Hello from {url}!");
  26.         });
  27.     }
  28. }
复制代码
方法二:UseUrls 方法

在 Program.cs 中使用 UseUrls 方法来指定应用程序的 URL。
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.Hosting;
  3. public class Program
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         CreateHostBuilder(args).Build().Run();
  8.     }
  9.     public static IHostBuilder CreateHostBuilder(string[] args) =>
  10.         Host.CreateDefaultBuilder(args)
  11.             .ConfigureWebHostDefaults(webBuilder =>
  12.             {
  13.                 webBuilder.UseUrls("http://localhost:5001", "https://localhost:5002"); // 在这里指定端口号
  14.                 webBuilder.UseStartup<Startup>();
  15.             });
  16. }
复制代码
方法三:命令行参数方法

通过命令行参数在启动应用程序时指定端口。
  1. dotnet run --urls "http://localhost:5001"
复制代码
方法四:host.json 配置方法

在项目根目录下创建 hosting.json 文件,指定端口配置。
  1. {
  2.   "urls": "http://localhost:5001"
  3. }
复制代码
方法五:使用 Docker 方法

在 Dockerfile 中设置环境变量,然后在 docker-compose.yml 文件中映射端口。
Dockerfile:
  1. FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
  2. WORKDIR /app
  3. COPY bin/Release/netcoreapp3.1/publish/ App/
  4. ENTRYPOINT ["dotnet", "App/YourApp.dll"]
复制代码
docker-compose.yml:
  1. version: '3.4'
  2. services:
  3.   web:
  4.     build:
  5.       context: .
  6.       dockerfile: Dockerfile
  7.     ports:
  8.       - "5001:80"  # 映射容器端口到主机端口
复制代码
通过以上五种方法,你可以根据需要选择适合你的项目的方式来修改 ASP.NET Core 应用程序的默认端口。请根据你的需求选择其中一种或多种方法。
 


来源:https://www.cnblogs.com/hanbing81868164/archive/2023/12/12/17896989.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具