Search code examples
reactjs.netasp.net-core

.NET published app running different ports than Development


When I debug my React .NET project, two terminals open. One for React, one for .NET backend. Now when I publish the app to a folder I get a wwwroot & runtime folder, a shitload of dlls and one exe file.

So I understand that one terminal doesn't come up anymore since the react app is now just a folder like a normal html website.

When I debug the website I get:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7291
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5026
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\renek\source\ReactNet\ReactNet
info: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
      No SPA development server running at https://localhost:44411 found.
info: Microsoft.AspNetCore.SpaProxy.SpaProxyMiddleware[0]
      SPA proxy is not ready. Returning temporary landing page.
info: Microsoft.AspNetCore.SpaProxy.SpaProxyMiddleware[0]
      SPA proxy is ready. Redirecting to https://localhost:44411.
info: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
      SPA development server running at 'https://localhost:44411'

Now when I publish the project to my VPS and run the exe:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\inetpub

Can someone explain how I can get the settings the same?

What I've tried so far:

  • Launchsettings.json change the values from Development to Production:
{
  "profiles": {
    "ReactNet": {
      "commandName": "Project",
      "launchBrowser": false, // True originally
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production", // Development originally
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      },
      "applicationUrl": "https://localhost:7291;http://localhost:5026"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": false, // True originally
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production", // Development originally
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }
    }
  },
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:32385",
      "sslPort": 44344
    }
  }
}

YES I get that editing launchSettings is only for Development. This is my first project I'm actually publishing so don't blame me for not knowing exactly how to.


Solution

  • If you want to keep the port when you run the .exe ,you may try:

    var builder = WebApplication.CreateBuilder(args);
    builder.WebHost.UseUrls("http://localhost:5026;https://localhost:7291");
    

    Here's the document related

    The result:

    enter image description here