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

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

Related

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

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:

Adding a razor page in Blazor Project creates a .cshtml file

I'm following a tutorial trying to create a blazor project.
It says to add a razor page. When I right click on Pages folder ->Add->Razor Page, I get this prompt.
No matter what options I pick, the new file is a .cshtml file, not a .razor file.
I see other .razor files in the same folder by default.
Target Framework: .NET Core 3.1
Am I missing something?
I'm using Microsoft Visual Studio Professional 2019, Version 16.4.3.
Instead of creating a new "Blazor Page", go to the dialog to add a "New Item". From ASP.NET Core, Select "Blazor Component". This will create a new file called YourComponent.razor.
Bonus Fact: To create a code-behind file for the component, add a new class and specify the name YourComponent.razor.cs. Declare the class partial (because the .razor file itself makes a partial class declaration).
I am also new to Blazor and this has been a source of confusion for me. I hope that they make it more straight-forward in the next version of Visual Studio.

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.

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

Can I use browserlink with an 'html' project?

I want to create an HTML project that works with browserlink. I created a blank c# project and set it to type 'class library' so that visual studio stops complaining that there's no 'main' entry point. I created a page index.html and setup a corresponding IIS website.
What do I do now? I can't start the project, because it's not a web project. Will this work, or do I need to create something like an MVC project?