简体   繁体   中英

Why automatic recompile doesn't work for C# classes in Visual Studio asp.net core mvc project?

I have an ASP.Net Core MVC simple project. When I start it without debugging (Ctrl + F5) and change any .cs File (Controller, for example), save it, and update the page in browser nothing happens. But it should automatically recompile and show changes, that was said in the book that I read.

Why can this happen?

I use Visual Studio 2019, .Net core 2.1.

If I manually close the page, and press start without debugging again it recompile and show changes in browser.

If I change .cshtml files (View) it takes effect as soon as I update the browser Page. So it is problem only with C# classes.

UPD dotnet watch works just fine for that purpose, but author of the book ( A.Freeman, Pro ASP.Net core MVC 2 7th edition, chapter 6 ) did not use it:

Visual Studio supports detecting changes as soon as an HTTP request is received from the browser and recompiling classes automatically. To see how this works, select Start Without Debugging. Once the browser displays the application data, make the changes to the Home controller. Save the changes to the controller class file and reload the browser window without stopping or restarting the application in Visual Studio. The HTTP request from the browser will trigger the compilation process, and the application will be restarted using the modified controller class

Why does this not work for me?

The book was written for Visual Studio 2017. It works as the book describes in VS 2017.

However, according to this , the auto-rebuild feature has not yet been added to Visual Studio 2019.


Original answer: (which applies when that feature is not available): The difference is that .cs files are compiled into your project's DLL at compile time. For any new code to take effect, the DLL needs to be updated, which will only happen if you specifically tell it to recompile. This is why, when you deploy your project, you copy over the .dll file and not the .cs files.

However, views (.cshtml files) stay as plain text files and are compiled when you use them. If you edit a .cshtml, it's recompiled the next time the view is used. This is why, when you deploy your project, you do need to copy the .cshtml files.

Visual Studio does have a feature to let you modify .cs files while debugging and have it take effect immediately, but it doesn't support ASP.NET projects. It's called Edit and Continue .

However, you can setup dotnet watch to detect file changes and restart your application. There are instructions on how to do that here: Develop ASP.NET Core apps using a file watcher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM