Specifying the url (port) that a asp.net core 1.0

2019-05-14 21:58发布

问题:

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.