Asp.Net Core Web API app: how to change listening address?

后端 未结 3 570
名媛妹妹
名媛妹妹 2020-12-15 11:16

I write simple Asp.Net Core WebAPI 2.0 application, it works on my local machine. But i want to deploy it to server. So, i do it.

My system:

Ubuntu 1         


        
3条回答
  •  悲哀的现实
    2020-12-15 11:56

    This is configured by Server urls host configuration

    Indicates the IP addresses or host addresses with ports and protocols that the server should listen on for requests.
    Key: urls
    Type: string
    Default: http://localhost:5000
    Set using: UseUrls
    Environment variable: ASPNETCORE_URLS

    Set to a semicolon-separated (;) list of URL prefixes to which the server should respond.


    It is also possible to set URL using command line arguments. For example:

    dotnet run --urls=http://0.0.0.0:5001

    but this doesn't work out of the box for old versions of ASP.NET Core (depends on whether this fix applied to used version or not).

    A workaround for old versions based on fact that you always can set host settings directly via .UseConfiguration method:

    var config = new ConfigurationBuilder().AddCommandLine(args).Build();
    
    return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
    

    Note, the same idea may be used to read setting value from any other configuration source, like configuration file for example.

提交回复
热议问题