7

I am a fairly new to developing applications on Mac OS. I am using Visual Studio for Mac 2022 - Preview. I am all up to date and have created a ASP.NET MVC Web Application using .NET 6. I have started the debugger using the generated project. I make a change to the Index.cshtml file but that change fails to be reflected in the browser after saving the file. Any ideas if Hot Reload works in Visual Studio for Mac? I dont see any button to toggle the Hot Reload as all screenshots i have seen are for Windows Visual Studio. Any help would be appreciated as stopping and starting the debugger each time a change is made is very inconvenient. Thanks.

EDIT: I have installed the Nuget Package

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

And then updated my Program.cs file as follows:

// Add services to the container. builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();

My changes to .cshtml files still do not want to recompile after saving.

Any more help would be appreciated.

Visual Studio for Mac Version: 17.0 Preview (17.0 build 5186)

Created a new Web Application (Model-View-Controller) project.

Nizzy
  • 379
  • 5
  • 14
  • See this: https://stackoverflow.com/questions/58173846/aspnet-core-3-0-and-3-1-enable-runtime-compilation-for-razor-pages – Dai Dec 25 '21 at 05:08
  • @Dai - I have updated my post. After reviewing the link you had suggested, i still wasnt able to get hot reload to work in my application. Any other ideas would be greatly appreciated! Thanks. – Nizzy Dec 25 '21 at 06:05
  • https://learn.microsoft.com/en-us/visualstudio/releases/2022/mac-release-notes-preview Not in release notes, so not likely to be available at this moment. – Lex Li Dec 25 '21 at 17:43

3 Answers3

9

I got it to work on the latest preview version of Visual Studio 2022 for Mac. You will need to add the NuGet package, and also add the service to your builder in the program.cs file as an extra service:

builder.services.AddRazorPages().AddRazorRuntimecompilation();

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-6.0&tabs=visual-studio#enable-runtime-compilation-for-all-environments

Ateik
  • 2,458
  • 4
  • 39
  • 59
vamplew
  • 91
  • 1
  • 2
  • Thanks you for sharing. For some reason just can't get it to work on latest VS2022 for Mac. Wondering if something changed recently and requires additional steps. – AlexVPerl Sep 11 '22 at 22:47
4

I don't know of any way to do it within Visual Studio, but if you navigate to the folder with your .csproj in Terminal, you can type

dotnet watch

and it will load your app. Then you can make changes within Visual Studio, save, and the browser will automatically reload. The only problem is since it's running from the command line, it won't hit breakpoints in your program (I don't know of any way to do so anyway).

Misguided Chunk
  • 161
  • 1
  • 10
0

As mentioned above, adding builder.Services.AddRazorPages().AddRazorRuntimeCompilation() works well, the only thing that you have to reload the page manually though, while your project is running.