简体   繁体   English

指定asp.net核心1.0 WebAPI.exe应该在program.cs中为prod和dev使用的url(端口)

[英]Specifying the url (port) that a asp.net core 1.0 WebAPI.exe should use in program.cs for both prod and dev

I am doing the following in my asp.net core 1.0 web api (.NET Framework) program.cs to specify which port I want my web api exe to run in for development purposes only: 我在我的asp.net core 1.0 web api(.NET Framework)program.cs中执行以下操作,以指定我希望我的web api exe运行的端口仅用于开发目的:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls(new string[1] { "http://*:12012" })
        .Build();

    host.Run();
}

However, when I publish to production this line causes the WebAPI to error since I want the exe to use the production web-api url ie productionWeb/api/values rather than localhost:12012/values 但是,当我发布到生产时,这行会导致WebAPI出错,因为我希望exe使用生产web-api url即productionWeb / api / values而不是localhost:12012 / values

Is there anyway I can get the best of both worlds being able to specify that I want it to run on port 12012 for development purposes and the production url for prod purposes? 无论如何,我可以得到两个世界中最好的,能够指定我希望它在端口12012上运行用于开发目的和生产URL用于产品目的吗?

My current solution is to just comment out the line before publishing. 我目前的解决方案是在发布之前注释掉该行。

When using IIS you are overwriting the url the IIS (AspNet Core Module) told the app to listen to by calling .UseUrls() after .UseIISIntegration() . 当使用IIS要覆盖URL中的IIS(ASPNET核心模块)告诉应用程序通过调用听.UseUrls().UseIISIntegration() You should change the order of these two calls so that .UseIISIntegration() is after .UseUrl() . 您应该更改这两个调用的顺序,以便.UseIISIntegration().UseUrl() .UseIISIntegration() will not touch urls you set if you are not running with IIS so in development your application still will be listening on port 12012. When running with IIS .UseIISIntegration() will overwrite the url to listen on the port IIS told it to listen on. 如果你没有运行IIS,则.UseIISIntegration()将不会触及你设置的网址,因此在开发中你的应用程序仍然会在端口12012上侦听。当运行IIS时.UseIISIntegration()会覆盖url以侦听端口IIS告诉它听。 I wrote a post on running Asp.NET Core apps with IIS and Azure Websites which explains how things work including this nuance. 我写了一篇关于使用IIS和Azure网站运行Asp.NET核心应用程序文章,该文章解释了包括这种细微差别在内的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM