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

后端 未结 12 982
误落风尘
误落风尘 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:27

    In Asp.net core 2.0 WebApp, if you are using visual studio search LaunchSettings.json. I am adding my LaunchSettings.json, you can change port no as u can see.

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

    Use following one line of code .UseUrls("http://*:80") in Program.cs

    Thus changing .UseStartup<Startup>()

    to

    .UseStartup<Startup>() .UseUrls("http://*:80")

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

    If you want to run on a specific port 60535 while developing locally but want to run app on port 80 in stage/prod environment servers, this does it.

    Add to environmentVariables section in launchSettings.json

    "ASPNETCORE_DEVELOPER_OVERRIDES": "Developer-Overrides",
    

    and then modify Program.cs to

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseKestrel(options =>
                        {
                            var devOverride = Environment.GetEnvironmentVariable("ASPNETCORE_DEVELOPER_OVERRIDES");
                            if (!string.IsNullOrWhiteSpace(devOverride))
                            {
                                options.ListenLocalhost(60535);
                            }
                            else
                            {
                                options.ListenAnyIP(80);
                            }
                        })
                        .UseStartup<Startup>()
                        .UseNLog();                   
                    });
    
    0 讨论(0)
  • 2020-12-05 02:31

    Yes this will be accesible from other machines if you bind on any external IP address. For example binding to http://*:80 . Note that binding to http://localhost:80 will only bind on 127.0.0.1 interface and therefore will not be accesible from other machines.

    Visual Studio is overriding your port. You can change VS port editing this file Properties\launchSettings.json or else set it by code:

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseUrls("http://*:80") // <-----
                .Build();
    
            host.Run();
    

    A step by step guide using an external config file is available here.

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

    in your hosting.json replace"Url": "http://localhost:80" by"Url": "http://*:80" and you will be able now access to your application by http://your_local_machine_ip:80 for example http://192.168.1.4:80

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

    Maybe it's because I am not using Core yet. My project didn't have a LaunchSettings.json file but that did prompt me to look in the project properties. I found it under the Web tab and simply changed the project url:

    0 讨论(0)
提交回复
热议问题