I am writing my first ASP.NET Core 2.0 web REST API application by following this tutorial. However my specific question is about the code you get in the Program.cs file when you create a standard ASP.NET Core Web Application in VS2017, it is the same code described here:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace WebApplication5
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
I have my app working fine when I debug in VS2017, so next step that I did was to make it a standalone app according to this tutorial, which works fine and gives me an executable I can run (I'm on windows 10 x64).
The problem now is that this executable starts the webserver on port 5000, but I'd like to be able to configure the listening urls via a command line parameter.
By looking at the code above, we can see that args
is passed to WebHost.CreateDefaultBuilder(args)
, so I'm assuming any command line arguments are interpreted by this function, however I cannot figure out what I have to pass on the command line in order to get the server to listen on another port.
I have tried the following options:
- MyApp.exe --UseUrls="http://*:5001"
- MyApp.exe --UseUrls=http://*:5001
- MyApp.exe --server.urls=http://*:5001
- MyApp.exe urls="http://*:5001"
And various other combinations like that... The app starts but keeps listening only on port 5000.
I'm beginning to think I'm trying something which is not possible :) So is it really not possible or am I missing something?
In linux I am using: ./MYAPP urls=http://*:8081 &
, but you need to modify your code for that. Try changing your code accordingly:
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var configuration = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(configuration)
.UseStartup<Startup>()
.Build();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With