How to automatically reload .NET Core project in Visual Studio 2019

22,547

Solution 1

run this command in project console

dotnet watch run

same works for visual studio code

From Develop ASP.NET Core apps using a file watcher (for 3.0)

dotnet watch is a tool that runs a .NET Core CLI command when source files change. For example, a file change can trigger compilation, test execution, or deployment.

The link above contains a tutorial with two sample projects:

  1. WebApp (an ASP.NET Core web API) and
  2. WebAppTests (unit tests for the web API).

Alternatively, you can also this nuget package for runtime compilation.

Solution 2

I think that dotnet watch should work. See the documentation from the link as there are various options.

  1. Add Microsoft.DotNet.Watcher.Tools to the tools section of the project.json file
  2. Run dotnet restore
  3. Execute with dotnet watch run

Solution 3

You can use dotnet watch. Viz. Docs

I have created custom lunch profile to make it easyer to run in VS.

"Watch": {
  "executablePath": "dotnet.exe",
  "workingDirectory": "$(ProjectDir)",
  "commandLineArgs": "watch run",
  "launchBrowser": false,
  "launchUrl": "http://localhost:5000/",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

Solution 4

According to this requirement, we need to run .net core application just like Angular application, loading the pages and contents automatically without built and manual refresh.

I had done some research and experienced that auto-reloading is not possible in ASP.NET core project. However, we have got the success, in our solution we are using dotnet watch which monitors source files and if a file changes, shuts down the application that it started, rebuilds and publishes the project, and then restarts the application then we just need to refresh page manually in the browser to get the changes which made in application, we don’t require to build or start the project.

Steps to follow to use asp.net watch:

1) Create.Net core application.

2) Open a command Window in your Web project's folder

3) Type dotnet watch run

4) Open your browser and navigate to an API or Page

5) Make a change to source code

6) Save the file

7) Go back to the browser and refresh manually

8) You should see the change reflected

Solution 5

Use dotnet watch to recompile the source code. Use Browser Link with "Browser reload on save" from Visual Studio to reload all your browsers. https://marketplace.visualstudio.com/items?itemName=MadsKristensen.BrowserReloadonSave

Share:
22,547

Related videos on Youtube

Akash Limbani
Author by

Akash Limbani

I am a Sr. Software Engineer, with much enthusiasm and experience in developing software systems. I have the experience of leading, organizing, and successfully accomplishing software projects with a rich set of features and functionality. Especially, I have been successful in bending abstract system requirements into real software solutions having high security and performance. Currently, I am working with iMocha, Pune as a Sr. Software Engineer. I am a self-starter with excellent interpersonal, motivational, and facilitation skills.

Updated on July 09, 2022

Comments

  • Akash Limbani
    Akash Limbani almost 2 years

    I tried to automatically reload ASP.NET Core project as I do using Angular with Node or NPM.

    When I change the code of the .NET Core project and save, I want the web page to be automatically refreshed in the web browser.

    • Yasser Jarouf
      Yasser Jarouf over 4 years
      I'm not exactly sure what do you mean by reload but in general you can't change the code in runtime.
    • Akash Limbani
      Akash Limbani over 4 years
      @YasserJarouf, I am not talking about In the visual studio 2019.
    • Panagiotis Kanavos
      Panagiotis Kanavos over 4 years
      ASP.NET Core Angular projects already reload the client-side code - they are Angular, not just same as Angular. ASP.NET Core is used to provide the APIs and non-SPA pages.
    • Akash Limbani
      Akash Limbani over 4 years
      Any another ways, Like combine install npm package with ASP.NET Core project ?
    • Vikram Singh Saini
      Vikram Singh Saini almost 3 years
      I want to add a link Live Reloading Server And Client Side ASP.NET Core Apps with BrowserSync that might help future Googlers.
  • Akash Limbani
    Akash Limbani over 4 years
    But Using this, I am not able to reloading automatically like Angular. If I pressed ctrl+s, That time not any action working in browser. Thanks for giving answer.
  • PrinceT
    PrinceT over 4 years
    you have to run your application using command prompt and add watch details in project. json
  • GrowSing
    GrowSing over 4 years
    Or you can just use my launch profile I provided in my answer..
  • PrinceT
    PrinceT over 4 years
    You can see changes in console.. when you try to run.. are you able to see anything?
  • Akash Limbani
    Akash Limbani over 4 years
    Yes, But not reloading automatically in the browser like Angular.
  • Akash Limbani
    Akash Limbani over 4 years
    Using this, Not reloading automatically in the browser like Angular.
  • Panagiotis Kanavos
    Panagiotis Kanavos over 4 years
    @AkashLimbani like Angular doesn't do what you think it does. If you create use the Angular ASP.NET Core template, changes to the client code will be reloaded automatically. This is done because Node does the equivalent of dotnet watch and reloads the SPA when the files change.
  • Akash Limbani
    Akash Limbani over 4 years
    Can you run Angular project ?
  • Akash Limbani
    Akash Limbani over 4 years
    How to do in Visual Studio 2019 ?
  • NotAPro
    NotAPro over 3 years
    Can confirm this works in Visual Studio Code with ASP.NET MVC.
  • Lion
    Lion about 3 years
    Doesn't work for me on .NET Core 5 too. I needed to add "commandName": "Project" cause otherwise VS won't use it, but no reaction on file changes. When I run dotnet watch run outside of VS, it works well but sadly without any debugger :/
  • Lion
    Lion about 3 years
    I got the debugger started with "commandName": "Executable", and "executablePath": "dotnet.exe" but the debugger seems not properly attached: It doesn't hit breakpoints, which works fine with the default <ProjectName> project.