Home > Mobile >  I have React application running in ASP.NET Core project and want to run both at the same time
I have React application running in ASP.NET Core project and want to run both at the same time

Time:04-03

I created a default React/.NET Core project in Visual Studio and my launchSettings.json looks like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:28087",
      "sslPort": 44341
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "TTMApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

How do I run both the port 5001 api server and the React server at port 44341 at the same time? If I type dotnet-run it runs the 5001 app, but if I click "Start" or "IIS Express" in Visual Studio, it runs the 44341 React app. Is there a way to run both at the same time so I can test React queries to the API located at the 5001 port?

I've run React with Node and you can use Concurrently to run both at the same time, I'm just not sure how to achieve this in .Net Core without creating separate projects for React and for the API. I just have the OOB React Project from Visual Studio and it looks like it created everything in one project albeit with the React code at one port and the API at another.

CodePudding user response:

open Startup.cs and take a look at the configure method code.

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
        spa.UseReactDevelopmentServer(npmScript: "start");
    }
});

do you have this configuration? this way, the dotnet run command runs the react development in the background

  • Related