How to change the port number for Asp.Net core app?

后端 未结 12 981
误落风尘
误落风尘 2020-12-05 01:34

I added the following section in project.json.

  \"commands\": {
    \"run\": \"run server.urls=http://localhost:8082\",
    \"web\": \"Microsof         


        
相关标签:
12条回答
  • 2020-12-05 02:09

    All the other answer accounts only for http URLs. If the URL is https, then do as follows,

    1. Open launchsettings.json under Properties of the API project.

    2. Change the sslPort under iisSettings -> iisExpress

    A sample launchsettings.json will look as follows

    {
      "iisSettings": {
        "iisExpress": {
          "applicationUrl": "http://localhost:12345",
          "sslPort": 98765 <== Change_This
        }
      },
    
    0 讨论(0)
  • 2020-12-05 02:15

    We can use this command to run our host project via Windows Powershell without IIS and visual studio on a separate port. Default of krestel web server is 5001

    $env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
    
    0 讨论(0)
  • 2020-12-05 02:16

    you can also code like this

            IConfiguration config  = new ConfigurationBuilder()
            .AddCommandLine(args)
            .Build(); 
            var host = new WebHostBuilder()
             .UseConfiguration(config)
             .UseKestrel()
             .UseContentRoot(Directory.GetCurrentDirectory()) 
             .UseStartup<Startup>()
             .Build();
    

    and set up your application by command line :dotnet run --server.urls http://*:5555

    0 讨论(0)
  • 2020-12-05 02:20

    It's working to me.

    I use Asp.net core 2.2 (this way supported in asp.net core 2.1 and upper version).

    add Kestrel section in appsettings.json file. like this:

    {
      "Kestrel": {
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:4300"
          }
        }
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*"
    }
    

    and in Startup.cs:

    public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
          var builder = new ConfigurationBuilder()
             .SetBasePath(env.ContentRootPath)
             .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
             .AddEnvironmentVariables();
    
          Configuration = builder.Build();
    }
    
    0 讨论(0)
  • 2020-12-05 02:23

    In visual studio 2017 we can change the port number from LaunchSetting.json

    In Properties-> LaunchSettings.json.

    Open LaunchSettings.json and change the Port Number.

    Change the port Number in json file

    0 讨论(0)
  • 2020-12-05 02:25

    Go to your program.cs file add UseUrs method to set your url, make sure you don't use a reserved url or port

     public class Program
        {
            public static void Main(string[] args)
            {
                BuildWebHost(args).Run();
            }
    
            public static IWebHost BuildWebHost(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
    
                    // params string[] urls
                    .UseUrls(urls: "http://localhost:10000")
    
                    .Build();
        }
    
    0 讨论(0)
提交回复
热议问题