I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the project in order to see my changes. This has been happening for a while now so I'm not exactly sure what change caused this issue.
I've tried using the chrome's "Empty Cache and Hard Reload" as well as other browsers to no avail. This happens on Windows and Mac using both Visual Studio for Mac and VS Code
In a default .Net Core project it works fine so it must be something in my project that changed along the way. I'm wondering where I need to start in order to debug this issue? I've tried commenting out almost everything in my Startup.csand Program.cs with no resolution.
In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available.
Surprised that refreshing a view while the app is running did not work, I discovered the following solution:
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the
project
Add the following in Startup.cs:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Here's the full explanation for the curious.
There was a change made in ASP.NET Core 2.2 it seems (and I can't find any announcements about this change). If you are not explicitly running in the 'Development' environment then the Razor Views are compiled and you will not see any changes made to the .cshtml
You can however turn off this using some config in your Startup class as follows.
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
For ASP.NET Core 3.0 and higher, see Alexander Christov's answer.
I've just created a new project using the latest ASP.NET MVC Core 3.1 template and I altered the following to get runtime recompilation enabled for Debug:
Reference NuGet package - Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
Startup.cs - ConfigureServices(IServiceCollection services) WAS:
// stuff...
services.AddControllersWithViews();
// more stuff...
NOW:
// stuff...
var mvcBuilder = services.AddControllersWithViews();
#if DEBUG
mvcBuilder.AddRazorRuntimeCompilation();
#endif
// more stuff...
In addition to Alexander Christov's answer, since ASP.NET Core 3.1 you can enable the view compilation for development environment without changes to the Startup file:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Set next environment variables (for example via environmentVariables section in launchSettings.json):
ASPNETCORE_ENVIRONMENT to "Development".
ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".
You should just add this:
services.AddControllersWithViews();
to the ConfigureService method.
Be aware below code is not available in ASP.NET Core 3.1:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
For those using Net core 3.0 or greater
Go to Tools → Nuget package manager → Manage nuget pakages for solution
move to browse tab to browse from internet
search for RuntimeCompilation
Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
install it on your intended projects the current stable version
open Startup.cs file
go to void method ConfigureServices
add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();
you are DONE
Rerun and see. Now you can refresh your views or pages.
first of all install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation using nuget manager
after that add below code into your startup.cs
services.AddRazorPages().AddRazorRuntimeCompilation();
net6.0 also this works.
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change
Below helped me when views were in separate project.
if(HostingEnvironment.IsDevelopment()){ // only in development (optional)
services.AddMvc().AddRazorOptions(o => {
o.FileProviders.Add(new PhysicalFileProvider(PATH_TO_PROJECT));
});
}
I had the same issue while working on a .NET 6 MVC Web App.
I installed Microsoft.AspNetCore.Mvc.Razor.Runtime.Compilation from NuGet Package Manger then added .AddRazorRuntimeCompilation(); after
builder.services.AddControllersWithViews();
so that it looks like this
builder.services.AddControllersWithViews().AddRazorRuntimeCompilation();
and it worked!
Hope this helped.
I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development environment variable.
There are two ways to resolve this issue:
1. Check the permissions of folder in which your .sln file present.There may be a problem with file access permissions as Visual studio may not be to access files when IIS express server is running, so to reflect new .cshtml changes each time you need to restart the server,so I suggest edit the folder access permissions by:
Right click on folder->properties->security->click on edit button -> check all options->save.
Restart Visual studio to see changes.
If this does not work then use 2 option.
2.In your project in startup.cs file add this below line ConfigureServices() in method :
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
Are you absolutely sure you are using 2.2? Check your csproj because it might be this bug https://github.com/aspnet/Razor/issues/2466
You could try turning off RazorCompileOnBuild more info https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-2.1#properties
I had a similar problem upgrading from .net Core 3 to .net 5.0
Problem was due to old dependency in Telerik controls that we could not change.
Fixed by changing references in the .csproj file
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.8.0" />
to
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
(your version may be different)
In Visual Studio 2022 Preview, it seems there is an option called Hot Reload for this purpose.
It seems to be available in Visual Studio 2019 too.
With Hot Reload you can now modify your apps managed source code while
the application is running, without the need to manually pause or hit
a breakpoint. Simply make a supported change while your app is running
and in our new Visual Studio experience use the “apply code changes”
button to apply your edits.
https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/
Related
I have a .NET Core MVC app hosted in IIS (development) as well as Azure App Service (production).
When I make a simple HTML change to a Razor View and publish just that view, it does not get updated.
It only gets updated if I publish the entire project.
This happens in both IIS and Azure app service.
Is this the default behavior or am I doing something wrong?
Here is the configuration page from Azure App Service:
When you publish the complete program to iis, iis compiles and runs it. .net core mvc disables runtime compilation by default, so even if the view is updated and released, the program that is already running will not compile the new view.
If you want iis to use the new view after the VS update and release the view, you can add a line of code to the startup to enable the function of compiling and running.
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Nuget package to the project.
Add following code in startup.cs:
services.AddRazorPages().AddRazorRuntimeCompilation();
Publish entire project.
After all of these, once you update view and publish. IIS will display new view.
Here is my test result.
You don't need to do any operations on the portal.
The article provided by Lex Li talks about the content of Razor when compiling. Simply put, C# server code can be written in the .cshtml file. After compilation, it will become projectname.Views.dll, so when you modify When the .cshtml file is not sure which projects it is associated with, it is recommended to update it globally to avoid bugs caused by version issues.
Regarding your current problem of partial update, it is also easy to solve. First, you need to define the file or folder inclusion options when compiling. You need read offical document first.
Below screenshot is my test project.
After my modification and settings, you can publish your customized files or folders to the azure production environment. (The code setting part is for reference only, coding according to specific projects)
In the post, you said that you want to publish an html file, then you can right-click the file or folder and select publish file or folder.
Right click test folder.
From the screenshot message below, we can see that the partial update was successful, the speed is very fast, the modified content is also prompted, and the global update is not performed.
prompt:
The above steps are all tested and passed, and the answers and code parts given are for reference only.
If you encounter problems during the operation, it is recommended to raise a support ticket on the portal.
The package restore failed. Rolling back package changes in VS2019 for asp.net core.
When,I try to add API Controller using EF.
This didn't work for me
-Try to Clear All Nuget Caches ,
-Try clearing the ComponentModelCache
Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator"
It does not solve this error.
can anyone help me?
I had the same issue while adding the view for an IActionResult in .net 5.0 MVC using VS2019, before updating all the installed packages.
For me, restarting the Visual Studio works.
After restart I tried again, auto scaffolding installed this package "Microsoft.VisualStudio.Web.CodeGeneration.Design" along with generating a View.
For EF Core packages version 5.0.5 I was only able to solve this using the command line tool. See answer by johanjuulj here Visual Studio error: There was an error running the selected code generator
dotnet aspnet-codegenerator controller -m MyModel -dc MyDbContext -name MyModelssController -async
1 - Go to Tools
2 - Options
3- NuGet Package Manager
4- General
5 - Clear All NuGet Cache(s)
There are a couple of reasons that might account for this behaviour. Some of the reasons, as I have experienced on different occasions, include but might not be limited to the following:
Outdated Visual Studio Environment: In this case, you might attempt to update your version of VS.
Outdated Packages: In this case, update some or all of the packages in your project. To do this, go to Tools -> Nuget Package Manager -> Manage Nuget Package for Solution. Choose the packages that you want to update. if you are not sure, check Select all packages
In my case I needed to create an API controller and the only solution that worked for me was to download from the visual studio installer, in the individual features section: all the features of the SKD.NET framework,
all the .NET Framework support packages, ASP.NET and web development tools, and a few SQL features that I need.
I tried some of the suggestions above- but in my case I had all the versions of EF aligned on version 5.0.9 except for the autogenerated Swashbuckle.
I removed the following:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGenerators.Mvc" Version="6.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
then I had to reinstall (because it is used by my code):
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.9" />
Finally I ran the View scaffold again and now it worked!
I can see that during scaffolding the following package was auto installed:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
Similar issue in VS 2022. Trying to add an API controller to a separate ASP.Net Core Web API project in a solution with a separate project containing the POCO and DBContext. Tried some of the same fixes above; no luck.
I added an Api folder to the project containing the POCO and DBContext and added an API controller there with no problem. Then I copied the controller class to the Controller folder in the API project. I fixed the Route attribute in the controller, removing the "api/" part, added a connection string to the appsettings.json and called builder.Services.AddDbContext... in the API project's Program.cs.
This worked for me.
I had the same problem and tried many of the solutions but they didn't work. My project used .Net Core 3.1 and what fixed the issue was updating all NuGet packages in my project to the latest version of 3.1.x
Hope this helps.
I recently upgraded an .Net Framwork AspNet MVC app to a AspNet Core 3 MVC app and I'd like the ability to change a view, save, and refresh my browser window to see the changes. Now it appears I have to do a build every time before I can see any changes. Is there a way to change this behavior?
This is being hosted under IIS 10
As far as I know, the runtime compilation could just work in the develop environment. That means you couldn't use it in the production environment(which is hosted on the IIS).
If you change the visual studio's debug environment to IIS, it will stil work.
Besides, RuntimeCompilation is not a build-in feature in the asp.net core 3.0.
If you want to use it, I suggest you could try to install the package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and then configure AddRazorRuntimeCompilation in Startup.cs like
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}
There is a new way of doing this for 3.1, taken from: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1
Add the package at csproj
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.3" />
Then at launch.json, add a new environment variable
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
I was very happy to implement Westwind.AspnetCore.LiveReload per this blog post. It was quite easy and worked better than BrowserSync.
After some search I found a very simple solution:
Add following reference to csproj:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.3" />
Add AddRazorRuntimeCompilation() to your services configuration
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Context
In my .NET Framework 4.x ASP.NET MVC projects, when using the Visual Studio IDE, it was possible to edit a .cshtml view, save, then press ctrl+F5 in the browser and see the change immediately.
This seems to be no longer work in ASP.NET Core applications (using Visual Studio 2019 and .NET Core 3 Preview 5).
Question
Is this feature missing in ASP.NET Core? Is this a preview issue? Or am I missing something?
This is something that is no longer enabled by default as of ASP.NET Core 3, but it can be re-enabled as documented here:
Runtime compilation is enabled using the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. To enable runtime compilation, apps must:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:
services
.AddControllersWithViews()
.AddRazorRuntimeCompilation();
I've marked Kirk's answer as accepted, but maybe this experience could be useful also.
My goal was a "quick edit (cshtml/css/js/ts) then see" iteration.
No need to add and mod anything... I discovered that .NET Core (3) and VS 2019 is so much faster, so in case if we do not want to debug in VS (which is the scenario in most cases when we are changing cshtml/css/js/ts) there is really great iteration:
Press ctrl+f5 to launch your web app (no debug) in browser
See, test (debug js/ts in Chrome)
Edit your cshtml/css/js/ts
Press ctrl+shift+b to build (fast!)
Switch to your browser and press ctrl+f5 to refresh your page (fast!)
Goto 2
I am using .NET Core 1.0 and Visual Studio 2015 Update 3 to make a simple Asp.Net Core MVC website.
How can I debug my application and "Step Into" the .NET Core source code that is available on GitHub?
Specifically, I am trying to troubleshoot one issue with Microsoft.AspNetCore.Authentication.Facebook 1.0.0 assembly that I fetched from NuGet.
I wrote this article more than 1 year ago so it's a bit out of date but the idea is still the same:
You sync to the correct tag from GitHub. In your case, you probably want to sync to tag 1.0.0
Build that repository by running build.cmd or build.sh
Add the src folder path to your app's global.json file. For example, if you cloned Security in D:\Security, then you add D:/Security/src to global.json in the projects property.
Rebuild everything and it should work.
PS: If you use VS and don't see immediately the new code, try restarting it. It's a known issue that sometimes it doesn't pick up the changes to global.json
Update to fit new VS 2017
In Visual Studio 2017 15.3.5 and later
We can use SourceLink support for debugging .NET Core and ASP.NET Core sources.
To enable source link support just Disable Enable Just My Code and and Enable Enable Source Link Support.
Then Enable Microsoft symbol servers.
Victors answers works well with vs 2015.For some one who is looking for a solution with vs 2017,
In vs 2017 there is no global.json availble.So instead of adding folder path in global.json a project reference has to be added and rebuild.
All the others mentioned in Victors article works same as with 2015.
It is important that the git hub tag and the nuget package matches have the same versions.
Also make sure that on a solution level, you have your project "configration", set to debug. See screenshot. .
.
(For the solution properties to show up, right click on the .sln in the solution explorer.)
If you have debugged an app before with the previous version of .NET, delete the %TEMP%/SymbolCache directory as it can have old PDBs that are out of date. Per Debug .NET and ASP.NET Core source code