Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh View without restarting application

I used to be able to make changes to an ASP.NET (Core) MVC View and just hit refresh in the browser to apply the HTML (or Razor) changes.

Starting with ASP.NET Core 3.0 it seems I always have to restart the MVC application to get the latest changes in my browser.

This is my application configuration

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    app.UseStaticFiles();

    app.UseRouting();


    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
like image 618
Alexander Zeitler Avatar asked Dec 11 '19 11:12

Alexander Zeitler


People also ask

What is. net hot reload?

. NET Hot Reload applies code changes, including changes to stylesheets, to a running app without restarting the app and without losing app state. Hot Reload is supported for all ASP.NET Core 6.0 and later projects.

How do you reload a razor page?

If the Browser is refreshed using F5 button after the Form is submitted (in other words after PostBack operation), the submitted data is resubmitted to Server.


Video Answer


1 Answers

Adding the NuGet package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation to the project and changing this line fixed it:

services.AddControllersWithViews().AddRazorRuntimeCompilation();
like image 131
Alexander Zeitler Avatar answered Oct 18 '22 17:10

Alexander Zeitler