You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 6.0独立EXE程序端口与URL路径配置问询

ASP.NET Core 6.0独立运行EXE的端口与URL配置问题解答

问题1:修改独立运行EXE时的监听端口

有三种常用方法可以修改Kestrel的默认监听端口:

方法1:命令行参数临时指定

运行EXE时直接通过--urls参数指定端口,无需修改代码或配置文件:

# 示例:将HTTP端口改为8080,HTTPS端口改为8443
YourAppName.exe --urls "http://*:8080;https://*:8443"

方法2:通过appsettings.json永久配置

在项目的appsettings.json中添加Kestrel节点,配置固定端口,重新编译生成EXE后生效:

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:8080"
      },
      "Https": {
        "Url": "https://*:8443"
      }
    }
  },
  // 其他原有配置...
}

方法3:Program.cs硬编码配置(适合特殊场景)

在Program.cs中直接配置Kestrel的监听端口,编译后运行EXE会自动使用该配置:

var builder = WebApplication.CreateBuilder(args);

// 配置Kestrel监听端口
builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.ListenAnyIP(8080); // 监听所有IP的8080端口(HTTP)
    serverOptions.ListenAnyIP(8443, opts => opts.UseHttps()); // 监听所有IP的8443端口(HTTPS)
});

// 其他中间件、服务注册代码...

var app = builder.Build();
// 后续中间件配置...
app.Run();

问题2:配置访问URL为http://localhost/MyApp1、https://localhost/MyApp1形式

要实现带路径前缀的访问,可通过以下两种方式配置:

方法1:使用UsePathBase中间件

在Program.cs中添加UsePathBase中间件,指定全局路径前缀,同时配合Kestrel的URL配置:

var app = builder.Build();

// 设置全局路径前缀
app.UsePathBase("/MyApp1");

// 后续中间件(如UseRouting、UseAuthorization等)需放在UsePathBase之后
app.UseRouting();
app.UseAuthorization();

// 其他配置...
app.Run();

如果用命令行启动,需要在URL中带上前缀:

YourAppName.exe --urls "http://*:5000/MyApp1;https://*:5001/MyApp1"

或者在appsettings.json的Kestrel配置中加入前缀:

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:5000/MyApp1"
      },
      "Https": {
        "Url": "https://*:5001/MyApp1"
      }
    }
  }
}

方法2:全局路由前缀约定(适合MVC/API项目)

通过自定义路由约定,给所有控制器的路由自动加上前缀,无需修改Kestrel配置:

  1. 创建路由约定类:
public class RoutePrefixConvention : IApplicationModelConvention
{
    private readonly AttributeRouteModel _prefixRoute;

    public RoutePrefixConvention(string prefix)
    {
        _prefixRoute = new AttributeRouteModel(new RouteAttribute(prefix));
    }

    public void Apply(ApplicationModel application)
    {
        foreach (var controller in application.Controllers)
        {
            foreach (var selector in controller.Selectors)
            {
                if (selector.AttributeRouteModel != null)
                {
                    // 合并原有路由与前缀
                    selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_prefixRoute, selector.AttributeRouteModel);
                }
                else
                {
                    // 无原有路由的控制器直接使用前缀
                    selector.AttributeRouteModel = _prefixRoute;
                }
            }
        }
    }
}
  1. 在Program.cs中注册该约定:
builder.Services.AddControllersWithViews(options =>
{
    // 添加路由前缀约定,指定前缀为MyApp1
    options.Conventions.Add(new RoutePrefixConvention("MyApp1"));
});

配置完成后,访问控制器路由时就需要加上/MyApp1前缀,比如原路由/Home/Index会变为/MyApp1/Home/Index,根路径访问http://localhost:5000/MyApp1会指向默认路由。


内容的提问来源于stack exchange,提问作者ILIA BROUDNO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 14:42:56