thankyoussd
thankyoussd

Reputation: 2461

Can Blazor Server in Visual Studio hosted via Kestrel (not IIS Express) support automatic rebuild on Razor file change?

I'm using VS 2019, .NET 5 and Blazor server projects.

I always liked the plain Kestrel better than IIS Express when developing in VS 2019. It feels faster and I get the dedicated console window to show logs etc instead of relying on VS's output window. So every time I start a new project I usually switch from the default IIS Express to Kestrel via launchsettings.json.

However, I noticed that with IIS Express, Blazor server projects would auto re-build whenever a changed Razor file is saved. I do not see the same behavior after switching to Kestrel host.

Is there any way to make it work the same way as IIS Express for auto rebuild?

I see something about the dotnet watch, but I usually just launch quickly via Ctrl + F5 etc and it would be a pain to have to use command line every time to start the project.

Upvotes: 1

Views: 1227

Answers (2)

thankyoussd
thankyoussd

Reputation: 2461

Found the setting:

VS Options -> Projects and Solutions -> ASP.NET Core -> Auto build and refresh option

Change to "Auto build and refresh browser after saving changes".

Upvotes: 0

Mister Magoo
Mister Magoo

Reputation: 8994

You can use dotnet watch - and have Ctrl-F5 work, by adding a new profile to launchsettings.json in your project. You can select "Watch" in the toolbar as the profile, then press Ctrl+F5 to launch it.

    "Watch": {
      "commandName": "Executable",
      "dotnetRunMessages": "true",
      "workingDirectory": "$(ProjectDir)",
      "executablePath": "dotnet.exe",
      "commandLineArgs": "watch",
      "applicationUrl": "https://localhost:5000;http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

And when you move to .NET 6 with hot reload, you can use this:

    "HotReload": {
      "commandName": "Executable",
      "dotnetRunMessages": "true",
      "workingDirectory": "$(ProjectDir)",
      "executablePath": "dotnet.exe",
      "commandLineArgs": "watch",
      "hotReloadProfile": "aspnetcore",
      "applicationUrl": "https://localhost:5000;http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

Upvotes: 1

Related Questions