Search code examples
c#asp.net-coreprojects-and-solutionsriderrecompile

Recompiling an ASP.NET Core Application in Rider without repeatedly rerunning it


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?


Solution

  • 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.