首页 > 编程语言 >你知道如何修改ASP.NET Core默认端口吗?常用5种方法实例演示

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

时间:2023-12-12 15:25:11浏览次数:42  
标签:Core ASP 端口 Microsoft json using NET 方法 public

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

方法一:appsettings.json 配置 Kestrel 的 Endpoint

  1. 在 appsettings.json 中添加端口配置:
{
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://localhost:5001"
      }
    }
  }
}
  1. 在 Startup.cs 中读取配置:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // 添加服务配置
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // 其他中间件配置

        var url = _configuration["Kestrel:EndPoints:Http:Url"];
        
        // 使用 Kestrel Endpoint
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync($"Hello from {url}!");
        });
    }
}

方法二:UseUrls 方法

在 Program.cs 中使用 UseUrls 方法来指定应用程序的 URL。

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseUrls("http://localhost:5001", "https://localhost:5002"); // 在这里指定端口号
                webBuilder.UseStartup<Startup>();
            });
}

方法三:命令行参数方法

通过命令行参数在启动应用程序时指定端口。

dotnet run --urls "http://localhost:5001"

方法四:host.json 配置方法

在项目根目录下创建 hosting.json 文件,指定端口配置。

{
  "urls": "http://localhost:5001"
}

方法五:使用 Docker 方法

在 Dockerfile 中设置环境变量,然后在 docker-compose.yml 文件中映射端口。

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY bin/Release/netcoreapp3.1/publish/ App/
ENTRYPOINT ["dotnet", "App/YourApp.dll"]

docker-compose.yml:

version: '3.4'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5001:80"  # 映射容器端口到主机端口

通过以上五种方法,你可以根据需要选择适合你的项目的方式来修改 ASP.NET Core 应用程序的默认端口。请根据你的需求选择其中一种或多种方法。

 

标签:Core,ASP,端口,Microsoft,json,using,NET,方法,public
From: https://www.cnblogs.com/hanbing81868164/p/17896989.html

相关文章

  • .net批量插入到oracle数据库
    最近用到批量插入数据到oracle,总结了网上的方案 方式一:使用数组绑定https://www.codenong.com/343299/数据库:CREATE TABLE jkl_test (id NUMBER(9));USINGOracle.DataAccess.Client;namespaceOracleArrayInsertExample{classProgram{staticv......
  • AttributeError: 'NoneType' object has no attribute 'replace'
    在python中执行Js代码,引入execjs库,python代码如下:执行结果如下:在导入execjs模块前,让Popen的encoding参数锁定为utf-8。importsubprocessfromfunctoolsimportpartialsubprocess.Popen=partial(subprocess.Popen,encoding="utf-8") ......
  • .net formwork WebApi 跨域问题
    ASP.NETFormwork Api/ASP.NetCoreApi 做比较。有关 Global.asax、FilterConfig.cs和RouteConfig.cs等都被Program.cs和Startup.cs两个类取而代之.程序中把Program.cs作为Web应用程序的入口,程序启动的时候会调用Startup.cs类。Startup.cs作用就是,对项目......
  • Sitecore Item CURD
    基础CURDDatabasedb=Sitecore.Configuration.Factory.GetDatabase("master");Languagelang=Language.Parse("en");TemplateItemfolderTemplate=db.GetTemplate(" /sitecore/templates/Common/Folder");//获取HomeItemItemhom......
  • 工业生产中Profibus主站转Profinet网关优势所在
    应用案例分享:某制造企业拥有一条生产线,生产线上的多个设备之间需要进行数据通讯和相互控制。这些设备原本使用Profibus-DP总线进行通讯,但企业希望将这些设备集成到Profinet网络中,以便能够更好地实现设备监控和管理。我们为此提供了一款Profibus-DP主站转Profinet网关,将多个Profibu......
  • windowserver2012服务器部署.net core3.1环境
    一、安装.netcore3.1要先具备这些系统补丁,如果没有则需要安装,这些KB必须按以下顺序安装:(clearcompressionflag.exe、KB2919442、KB2919355、KB2932046、KB2959977、KB2937592、KB2938439、KB2934018)安装过程中需要多次重启生效。最后安装vc_redist.x64.exe)二、.netcore3.......
  • .net core sftp
    usingRenci.SshNet;///<summary>///上传文件到ftp服务器上///</summary>///<paramname="file">文件</param>///<paramname="dateStr">文件时间戳</param>///<returns&g......
  • 快速入门:使用 .NET Aspire 组件实现缓存
    前言云原生应用程序通常需要各种类型的可扩展缓存解决方案来提高性能。.NETAspire组件简化了连接到流行的缓存服务(例如Redis)的过程,今天小编就为大家简单介绍一下如何使用.NETAspire组件实现缓存。本文的内容概要:创建一个设置为使用.NETAspire的基本ASP.NETCore应......
  • JSON .NET SERIALIZE exploitation
    exploitingjsonserializationin.NETcore当使用特定的配置的时候,将在NewtonSoftJSON中会有json的反序列化漏洞。更加具体化一些就是当jsonserializationsettings中的typenamehandling这个属性不是None的时候。因为默认来说typenamehandling是设置成none的。当配置文......
  • .NET Conf 2023 Chengdu - 成都站圆满结束!
    今年的.NETConf2023,中国区首次有两个会场举办LocalEvent,成都会场已于上周六12月9日圆满结束。本次成都会场共计100+余名.NET开发者报名参与,共计10+名志愿者参与筹备,是成都.NET社区历年来活动之最,还吸引了来自重庆地区,甚至大连地区的开发者也来参与,衷心感谢大家对本次活动的支......