【问题标题】:Overriding applicationUrl when running `dotnet Myapp.dll` with netcore 2.2使用 net core 2.2 运行“dotnet App.dll”时覆盖应用程序 URL
【发布时间】:2019-03-11 01:29:11
【问题描述】:

假设我们采用默认 VS 模板之一生成的默认 asp netcore 2.2 应用程序。

运行dotnet publish --Release 后,我们会得到一个包含应用程序二进制文件的文件夹。

在默认http://localhost:5000 上运行dotnet MyDemo.dll 为应用加注星标。

如何更改默认端口和主机?

我试过设置ASPNETCORE_URLS环境变量没有效果。

一些额外的上下文:我知道对于本地开发,我们可以在 launchSettings.json 中设置不同的配置文件,我们可以使用dotnet run 命令来选择要运行的配置文件。但是,发布后没有 launchSettings.json 并且直接使用 dotnet MyDemo.dll 运行二进制文件似乎不允许任何额外的配置。

请参阅下面的 Startup 类。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseMvc();
    }
}

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    您应该在配置 Kestrel 网络服务器的位置拥有 Program.cs。在这些配置中,应该可以为主机名和端口指定 url。检查如下内容:

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseUrls("http://localhost:60000", "http://localhost:60001")
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();
    

    您可以在docs中找到其他配置端点的方法

    顺便说一句,对于 ContinuosIntegration/ContinuosDelivery,在 json 中具有端点设置会是更好的选择,所以也许您可以考虑改进您的 CI/CD 管道。

    还应该可以在命令行中使用this之类的东西指定url

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 2019-08-07
      • 2020-02-10
      相关资源
      最近更新 更多