How to avoid to recompile project every time I made a change to a view in .NET Core project? - asp.net-core-mvc

I am new to .NET Core 6 project using Razor views. One problem I am having now is that, every time I make a minimal change to a page, I need to recompile so that I can see the change in the browser.
I am using IIS Express as the server. That way, I can develop while the server is still running, but there is a thing left to solve, and it is to avoid to recompile the project when I change a view.
I realized if I make a change to a view, change is not seen until I recompile the project.
Any help?

I think what you want is Hot Reload:
Supported ASP.NET Core Scenarios
For ASP.NET Core developers who are targeting .NET 6, there are additional capabilities not available for lower versions of .NET. These capabilities include:
CSHTML: Editing a Razor CSHTML file supports many types of edits.
Browser Refresh: Editing a razor file automatically refreshes the changes in your web browser when debugging. This feature was
previously only available when starting the app without the debugger.
CSS Hot Reload: You can change CSS files while the app is running, and changes are applied immediately to the running app as you type.
No Debugger: You get Hot Reload support when using Visual Studio to start your web app without the debugger (CTRL-F5).
You can enable Hot Reload by going to Tools → Options → Debugging → .NET / C++ Hot Reload:

Related

is it normal that Hot Reload VS 2022 rebuild my project?

I am new in .net core 6 because I just finished migrating my project.
I would like to know if this behavior is normal about the native hot reload in vs 2022.
I am on a .Net Core 6 Web App and when I modify a razor page while i'm running without debugging (ctrl +f5), the whole web project is rebuilt and this results in a reload of the application, which for me is not really a hot reload.
Did I miss something ? Or is it the normal behavior ?
I finally found the issue here, looks like a bug because my project configuration is not named "Debug"

Hot Reload For CSS Not Working With Blazor WebAssembly Hosted

I noticed the following behaviour in Visual Studio 2022 (version 17.0.0) with a .Net 6.0 Blazor project:
Create a new Blazor WebAssembly project and DO NOT select the 'ASP.NET Core hosted' option.
Run the application without debugging.
Ensure Hot Reload is enabled
Make a change to the CSS. You will notice the browser instantly displays the change without needing a refresh / reload.
Repeat the steps above but this time during step 1 select the 'ASP.NET Core hosted' option. Now the CSS hot reload functionality is not working, even though making a change to the razor page itself does still hot reload (such adding or changing a HTML element).
According to the documentation*, hot reload is supported by WebAssembly when running without a debugger. I can't understand why only the CSS hot reload does not work if the 'ASP.NET Core hosted' option is chosen. The CSS and server appear to be unrelated.
*https://devblogs.microsoft.com/dotnet/update-on-net-hot-reload-progress-and-visual-studio-2022-highlights/
Ok, so this is what I did in order for an existing project (web assembly hosted version) to work with .net hot reload WITHOUT DEBUGGING:
Install Visual Studio 2022.
Download and install latest version of sdk and runtime. You could already have them but just to be safe:
SDK and Runtime
Make sure your solution is targeting .NET 6 or update the existing solution to .net 6. Follow the steps provided here: Update to .NET 6 from .NET 5
Go to Tools -> Options and check everything that is connected to hot reload including the CSS Hot Reload box like this:
Delete .bin and .debug folders from your solution, restart your PC and rebuild the solution after restart.
I was facing this problem while working with blazor server projects with .net 7 and this solution was very usefull for me and solved the problem for hot reloading on updating css files on blazor server projects.
Even you don't need to do refersh for your page.
https://www.nuget.org/packages/Toolbelt.AspNetCore.CssLiveReloader/1.1.4?_src=template
I hope this is useful for you.

ASP.NET MVC Custom View Path breaks Visual Studio shortcuts

I've seen a lot of articles on how to change the default paths for an ASP.NET MVC project so you can relocate or rename your Controllers, Models, and Views folders by extending the RazorViewEngine and specifying new path formats.
So far, this works great but the side-effect is that it has broken the Visual Studio shortcuts for "Go to View" or "Add View...".
Any ideas on whether those shortcut options can be customized to know about the new paths I've specified?
Thanks in advance! - Scott
The Visual Studio features cannot be extended to support custom paths. However, in the ASP.NET and Web Frameworks 2012.2 update that will soon be released (you can download a preview right now from here), the Add View functionality was updated to be a lot more flexible regarding which folders will have it enabled and which won't. It now supports Add View in any folder that is anywhere under a folder called "Views".
Regarding Go To View, that was not updated because unfortunately there is no way for the VS tooling to be aware of runtime customizations made to the view engine - I agree it would be cool, though!
(BTW my source for this information is that I work on ASP.NET MVC at Microsoft.)

Visual Studio 2010 gets stuck with solution with MVC3 Razor and Azure

I've a solution with several .dll projects, an Asp.NET MVC3 Razor project and an Cloud project.
If I try to compile the solution with the Cloud project loaded, VS2010 gets stuck in "Buil started".
I have to forcefully close VS2010 killing the process and restart. Then the compiling works good once, the next time it will get stuck again.
If I unload the project and set the mvc project as start-up, everything works (but azure of course).
What could be the problem?
Cheers.
Was it by any chance an ASP.NET MVC project to which you added a CloudService project later on or did you start off with a CloudService from the very beginning?
I was in the former situation and I had no end of problems, like not being able to open property pages or deploying the application. It's not quite your issue, but the following article might help:
http://tomkrueger.wordpress.com/2010/07/27/azure-deployment-issue-after-upgrading-to-visual-studio-2010-and-net-4-0/
As I say, my problems started because I actually added an Azure project to an existing ASP.NET MVC solution and there were some unnecessary settings left over in the web.csproj file.
All I had to do was open web.csproj in notepad and remove all occurrences of the <PlatformTarget> element.
My MVC prjoject was x86 and of course Azure works on x64 only. Even though I had the platform target set up as AnyCPU somehow Azure couldn't quite get along with it.
Maybe cleaning up your project files helps as well.

Visual Studio Keep Session between rebuilds etc

I would like to know if there is a way to keep the session going.
I've got sick of losing my session between rebuilds/builds, if I change too much in my project or submit code to our SVN (tortoise via VisualSVN). I'm developing an ASP.NET MVC project with lots of jQuery.
I would like to do this as I test my project on one screen with Firefox (Firebug wins) and edit on the other in visual studio. As my project has a session system and I would like to be able to develop without having to keep re-logging into my system.
Thanks
Edit:
Visual Studio 2008 Professional with the build in IIS development server.
Windows Vista Business 64bit
I don't think that's possible, given that the application restarts on each build. What you could do though, is add a temporary jQuery post call to the login page, that will run on startup.
$(function() {
$.post('/login/', { username: 'phil', password: 'noonewillguessthis' });
});
That way you'll be logged on to the system "automatically" each time the page loads. Just remember to remove it before you deploy live... ;)
It is not possible, when you rebuild and change the content of your libraries they have to be reloaded into the process space. However, in .Net you can never remove assemblies from an AppDomain once they are loaded. To reload your library the AppDomain needs to be unloaded and loaded again. In addition, if there are any changes to your web.config, it will also unload/reload your AppDomain.
You could use something like pushing session state to SQL server so that the AppDomain recycle doesn't impact you. You can do this with the sessionState element of the web.config file.
I have faced the same problem. I use VS 2015 on a machine. I rebuild/build project and on my first debugging the first generated session is lost. Think i mention about 2 paged project. first page is "Default.aspx", second page is "Home.aspx".
Default.aspx page is loaded first
Session["UserID"] is generated on Default.aspx
Page is redirected to Home.aspx by using Response.Redirect("Home.aspx",false) code.
Session["UserID"] is checked on Home.aspx.cs' s Page_Load function if it is existing or not
Session["UserID"] is not existing.. lost!
Solution:
I have instaled Visual Studio on other machine and I built the project on that machine. As result session missing problem is solved. Publishes are worked well on servers too. Maybe this problem is sourced by Visual Studio configurtions or machine configurations, I am not sure but installing VS may solve the problem.

Resources