2

I'm playing with vs code and a new asp.net core 2 webapi project. When I debug it or run it without debugging I can access the site from the same machine using http://localhost:5000 but I can't work out (and haven't found anything for the new .net core 2 way of doing things) to allow me to change the listening url to something like http://0.0.0.0:5000 or even http://*:5000 so I can access the dev site from another machine on the network to test with IE (it's running on a Mac).

I have found plenty of examples for .net core <2 that show editing the Program.cs file and adding a host config but that file has changed now (simplified) and I can't find or work out a way to apply the same changes to the new layout.

As well as other questions and solutions like this that modify a hosting.json or project.json which both appear from my reasearch to have been removed or more replaced with the .csproj file. And again I have been unable to work out or find information on how to implement the same changes in the new .csproj file.

So could someone please point me in the right direction. I expected changing the host/listening url to be a LOT easier than this so I'm sure I am missing something obvious.

GazB
  • 3,510
  • 1
  • 36
  • 42
  • I found this answer: https://stackoverflow.com/a/39733403/2698119, but I couldn't make it work. Maybe it will work for you though. – Métoule Jan 27 '18 at 16:32
  • Thanks @Métoule but that is again the same code for the old .net Code 1.# from what I understand that has changed for core 2.# when the program.cs changed. See the section half way down called: "Update Main method in Program.cs" in this: https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/ You can see the change from what that answer shows to what is there now. :( – GazB Jan 27 '18 at 19:02
  • The `CreateDefaultBuilder` method is meant to simplify the boilerplate code, but underneath, it looks to be identical to 1.x : https://github.com/aspnet/MetaPackages/blob/7511a4da7f1d1d9651d19801aadea77f557e0b11/src/Microsoft.AspNetCore/WebHost.cs#L148 – Métoule Jan 29 '18 at 08:32

3 Answers3

1

Update:

I have just come across the Environment variables which seem a FAR better place to set this, also this is how I would have done it in VS 2017.

In JetBrains Rider they are under the build configs (Run dropdown then Edit Configurations...).

Run - Edit Configurations

Under this window you can edit your build configurations. Find the one you use (I only have one currently but you might have a dev, staging etc...) and then find the Environment variables: (green) and click the button on the right (blue).

Run/Debug Configurations

In the new window you can change the ASPNETCORE_URLS to what ever you require. For me I set it to http://*:5000 which causes it to pick up any incoming requests. For example localhost:5000 or 192.168.0.10:5000. I then pointed my dev.somedomain.com to 192.168.0.10:5000 and I can use https though NGINX to test on my dev site running on my Mac.

enter image description here

This also allows for easily changing it on a PC my PC basis by not checking in the JetBrains Rider settings.


Original Answer:

I have finally found the answer to this and @Métoule was right in the comment. I just didn't understand what it meant until now.

Basically the changes in the Program.cs in ASP.NET CORE 2.0 is just a way to hide away the stuff that will always be the same. Instead of calling all these (like you needed to in Core 1):

.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();

You're just calling the ones you're likely to want to change:

.UseStartup<Startup>()
.Build();

SO you can still add UseUrls() as before just put it before the .Build();` like this:

.UseStartup<Startup>()
.UseUrls("http://192.168.2.10:5000")
.Build();

SO to change the Url of a brand new ASP.NET CORE 2 project I changed the Program class in the Program.cs file from:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

To:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("http://192.168.2.22:5000")
            .Build();
}

Only adding the 3rd from last line: .UseUrls("http://192.168.2.22:5000") As long as there are no firewalls in the way I can now access the above url from another machine on the network and see my dev site! :D

Community
  • 1
  • 1
GazB
  • 3,510
  • 1
  • 36
  • 42
1

Here is my program.cs class in .NET Core 2.0 :

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace MyNamespace
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://*:5000")
                .Build();
    }
}

Note the .UseUrls("http://*:5000") that accept any incoming requests from port 5000. This is only used when you publish your application (right click on the projet, publish...).

When you run your application from VS 2017 (for exemple), VS takes the settings under Properties/launchSettings.json. You can edit this file anytime.

Hope this will help someone else because I wasted too much time on this!

Wingjam
  • 742
  • 8
  • 17
  • I'm not sure what this is adding to my answer? – GazB Mar 06 '18 at 10:03
  • Mainly the possibility of using directly `.UseUrls("http://*:5000")` in `program.cs` instead of adding `http://*:5000` in the environment variable ASPNETCORE_URLS. – Wingjam Mar 06 '18 at 16:23
  • So like my original answer? ;) Well bar replacing the IP with wildcard as covered in the newer part of my answer. – GazB Mar 06 '18 at 19:41
0

This may be a bit late to the conversation, but if you are still trying to get this to work with VS Code, you are able to set ENVIRONMENT variables directly in your VS Code launch.json.

    {
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development",
                "ASPNETCORE_URLS": "http://*:5000"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}
Filosopher
  • 83
  • 8