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

天涯浪子 提交于 2019-12-07 12:08:22

问题


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:

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 i.e. productionWeb/api/values rather than 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?

My current solution is to just comment out the line before publishing.


回答1:


When using IIS you are overwriting the url the IIS (AspNet Core Module) told the app to listen to by calling .UseUrls() after .UseIISIntegration(). You should change the order of these two calls so that .UseIISIntegration() is after .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. I wrote a post on running Asp.NET Core apps with IIS and Azure Websites which explains how things work including this nuance.



来源:https://stackoverflow.com/questions/37862475/specifying-the-url-port-that-a-asp-net-core-1-0-webapi-exe-should-use-in-progr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!