Why do I need to enable razor runtime compile? Am I taking crazy pills? - razor

As far as I know I've always been able to update cshtml files and immediately see changes (upon refreshing browser) without having to stop my project.
But as I am creating a new project I see this new option to enable this feature and I'm wondering what it is because, after all, I've always been able to do this. But as far as I can tell it does exactly what it says... as if I wasn't able to do this in the past?
But I also notice now that when I make a change to a razor page, it takes a looot longer to see the update as Visual Studio recompiles the project.
What... is... going... on?

As far as I know I've always been able to update cshtml files and
immediately see changes (upon refreshing browser) without having to
stop my project.
Yes.Before asp.net core 3.0,you could do it by default.After asp.net core 3.0,you could install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to enable runtime compilation.
1.Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
2.Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-5.0&tabs=visual-studio#enable-runtime-compilation-in-an-existing-project
Update:
From asp.net core 3.1,you could select the Enable Razor runtime compilation checkbox in the Create a new ASP.NET Core web application dialog:

Related

compile razor views in .net core

Is there a way I can compile all my razor views (to verify) any time I need? I found this doc which shows how it compiles on publish https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-2.1&tabs=aspnetcore2x.
I am looking for an option within visual studio or even better via CLI that compiles and validates all views. I did find this official razor CLI tool in preview, but no documentation: https://www.nuget.org/packages/Microsoft.AspNetCore.Razor.Tools/1.1.0-preview4-final
Well, you can publish any time you need. The name ‘publish’ does not mean ‘push my site to the coliseum of public opinion’ :-)
All that it does stays local, and dotnet publish will by default create a directory under your project bin folder with, as you say, compiled views and other artefacts.
Per this comment, with .NET core 2.1 you can compile razor views at build time without needing to publish, by adding these two lines to the <PropertyGroup> section of your project file:
<RazorCompileOnBuild>true</RazorCompileOnBuild>
<ResolvedRazorCompileToolset>RazorSdk</ResolvedRazorCompileToolset>
This will cause them to compile to a [project].Views.dll, and you'll no longer need to distribute the cshtml files.

Why I need to Rebuild whole solution when make change in razor cshtml file

I wonder why every time I need to rebuild whole solution when I make changes to .cshtml file to see changes on web. It takes so much time and it is annoying when I just want to check some minor change in html (for example adding <td> element into table which will be filled with value from model)
Please can somebody explain it to me? I'm not very experienced in this area and I want to understand it more :)
Late for the party?
I had the same issue in VS 2019 RC2 while working with out of the oven, still warm demo project based on the Razor Components App template. For each change to take effect I had to rebuild the project. I excluded browser cache issues and everything else I could think of or found mentioned on the web.
I found that in the .csproj file the razor file extensions was set to .cshtml:
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<_RazorComponentInclude>Components\**\*.*cshtml*;</_RazorComponentInclude>
</PropertyGroup>
...while the actual file extensions were .razor. After renaming the .razor files to have .cshtml extension everything worked as expected.
Note, it didn't work the other way around, editing the .csproj file to look for .razor extension because there was a conflict in two stock .target files that I didn't want to touch.
That depends on the change.
If it is a razor change, yes you need to build (compile dlls). If it is not, that you don't.
Razor syntax is a view syntax of c#. It is used in views and transformed into html via the Razor View Engine.
By being C# code, you need to generate it's IL by doing a build or rebuild. Basically you are telling the compiler to generate a new IL based on your changes. Than the web server uses the dlls to run the app.

TagHelpers Intellisense not working with dnx46 in vNext beta8

I am not able to get TagHelpers Colorization and Intellisense to work in ASP.NET vNext WebToolsExtensionsVS14 Beta 8 when using the dnx46 framework.
NOTE: The project does build, run and TagHelpers are correctly processed on the server. This issue only affects design-time.
Steps I used:
File >> New Project - with the default project options (using .Net Framework 4.6) and Web Application template:
View the _Layout.cshtml, _ViewImports.chhtml, and project.json as follows:
Colorization and Intellisense is working
Modify the project.json frameworks from "dnx451" to "dnx46" and save. Allow the references to update. Close the _Layout.cshtml. Rebuild the solution.
View the _Layout.cshtml:
Colorization and Intellisense NOT working
Questions:
Is anyone else having this issue?
Has anyone figured out a work around?
Known issue. It's being tracked here
This issue has been resolved in the RC1.
https://docs.asp.net/en/latest/getting-started/installing-on-windows.html

How to set up visual studio to create CSHTML files in (Umbraco) Project

So, i'm developing my razor macroscripts in Visual studio for my Umbraco project.
Everything is working fine, but there are two things really annoying.
If I want to make a new CSHTML file the best solution for this is to duplicate an existing file.
I dont have full razor IntelliSense like e.g. Html.Raw
Is there a way to configure my project to use this features? Didn't find a .cshtml template yet.
You need to have the MVC Framework installed, then when you open the project as a website, you should be able to create and edit cshtml files with syntax highlighting. See my answer to the following post for more details:
Setting up local development environment for Umbraco
If your project is a web site/application then the mvc templates aren't available (they only show up in MVC projects). You can just create a text file and name it with the .cshtml extension though (you could set up your own template for this in VS if you wanted to).
To get intellisense in your Razor files, see Doug Robar's blog post on the subject
As an alternative if you go into the Umbraco admin, go to the 'Developer' section and right click on 'Scripting Files' you can create razor scripts directly (and this will save the new .cshtml directly into your 'macroScripts' folder - although in VS2010 you will need to right click on the new script and choose 'include in project').
Also this will allow you to base your new razor macroscript on one of the pre-built snippets so you may get a bit of core functionality for free.
From Umbraco 6 on it's very convenient to install Umbraco on your local file system with Visual Studio and NuGet. Given that you have the MVC Framework installed and you use Visual Studio 2012 or above, you get full Razor support in Visual studio.
Umbraco Our has a great blogpost about this where they described the steps below in detail (with screenshots!).
Create an Empty Web Application.
Install Umbraco using Manage Nuget Packages ('Umbraco CMS') or the Package manager console (Install-Package UmbracoCms)
NuGet will then download dependencies and will install all of Umbraco's files in your new solution. During this process it will ask if it is allowed to overwrite your web.config file. (Make a back up of your existing web.config if you install Umbraco in an existing project)
Finally, don't forget to run your project hitting F5. You'll see that whenever you try to add or edit a file in your views folder you have razor support and intellisense

Playn HTML5 won't run from Eclipse

I am trying to run the Playn example projects. I followed every step in this guide to setup new Playn development environment and then this guide to run sample projects.
it seem to work fine but when I try to run the HTML5 version by right click and then going to Google-> GWT compile, nothing happens. I don't see the development mode view poping up to copy the address and paste it to web browser as the guide says. I just get the following in the console window:
Compiling module playn.showcase.Showcase
Compiling 1 permutation
Compiling permutation 0...
Compile of permutations succeeded
Linking into L:\playn-samples\showcase\html\war\showcase
Link succeeded
Compilation succeeded -- 35.187s
Beyond that nothing happens. If I right click and select run as-> web application, I get the pop out saying
Could not find any hosting pages in the project playn-showcase-html
Anybody know what am I doing wrong ?
What you got from the compilation was all good.
"Beyond that nothing happens." is okay.
When you right click on the "playn-showcase-html" project, select "Run As - (g) Web Application", you should get an output to the "Development Mode" tab as "http://127.0.0.1:8888/Showcase.html?gwt.codesvr=127.0.0.1:9997".
If not, check if you have got the following folder structures under the "playn-showcase-html" project:
playn-showcase-html
|...
|--war
|--Showcase.html
|--WEB-INF
|--web.xml
As far as I know, you can't simply compile (GWT) and run the HTML version. This is because, the HTML version requires a local web server (such as jetty/tomcat) to host the files in order for the project to be 'run'. However, a simpler way around this would be to try using ant via Eclipse.
Window > Show View > Ant
Once the window appears (probably on a sidebar), right-click and select:
Add Buildfiles...
When the list of projects appear, expand the project by clicking the small arrow to the left of the project name in the list. Then select the ant build file:
build.xml
That will add the ant build file to your list of active build files.
Expand similarly to look at the ant tasks provided by the build file.
Double click on the appropriate task; in your case:
run-html
OR
Run ant directly on the command-line to get the same results.
In the current version of PlayN a jetty server is being started automatically. Right click on the xx-html project "Run As"->"maven install". This starts the GWT compiler, and starts a jetty server (default port is 8080), then you can run the HTML5 version by typing "localhost:8080" in yout browser.
I've also wrote a more detailed description about this on my blog getting started with eclipse and PlayN , maybe this could be interesting.