3

I'm working on an ASP.NET Core MVC application in Rider in which I'm frequently editing the code and as a result having to repeatedly manually stop and start the application, which is highly inconvenient.

To solve this, I tried various approaches:

  1. Rebuilding the application while running doesn't work. First, I get a warning: warning If I click build, the solution is rebuilt, but any code changes aren't reflected in the running web server. E.g if I changed the string "welcome" to "goodbye", rebuilding still shows welcome after rebuilding.
  2. dotnet watch run. Adding this command as an external tool to run in the pre-launch configuration does get save-based recompilation working. However, this doesn't integrate with the built-in runner. Since dotnet watch run starts its own web server, Rider's run command never actually runs. As a result, Rider doesn't detect that the application has started running, but instead thinks that dotnet watch run is some pre-launch task to begin before running. With debugging, I am unable to hit any breakpoints for this reason as well.

Is there any other way I can quickly rebuild an ASP.NET Core project while running it?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88

1 Answers1

8

I think what you're after is Razor file compilation. This will rebuild your views upon editing them without restarting the server.

If you add this package reference:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />

and add this snippet to your startup:

public IWebHostEnvironment Env { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    IMvcBuilder builder = services.AddRazorPages();

#if DEBUG
    if (Env.IsDevelopment())
    {
        builder.AddRazorRuntimeCompilation();
    }
#endif

    // code omitted for brevity
}

you should now have the views being rebuilt. You can read more about this in the docs.

EDIT:

If you would also like to have all files updated (not just views), there are a couple of options:

  1. You can add a Live Reload middleware using this package.

  2. You can use dotnet-watch and to rebuild and run the project when it is saved. See this answer on how to do that.

marcusturewicz
  • 2,394
  • 2
  • 23
  • 38
  • I was aware of this, but doesn’t it only apply to view files? Ideally, I was looking for something that would allow me to do this with regular files as well. – Arnav Borborah Jan 07 '20 at 01:58
  • Correct, this is only for views. For regular files i.e. live reload, there is this community option https://github.com/RickStrahl/Westwind.AspnetCore.LiveReload. The built in option is being tracked at https://github.com/aspnet/AspNetCore/issues/5456. – marcusturewicz Jan 07 '20 at 02:04
  • In addition to the live reload, I found a way to integrate the `dotnet watch` script better with Rider by [adding it as a launch profile](https://stackoverflow.com/a/53225064/6525260). I think you should add it to your answer for completeness. Thanks! – Arnav Borborah Jan 07 '20 at 15:21