compile razor views in .net core - razor

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.

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:

Partial Views in a Razor Class Library aren't found

I have a simple RCL with following structure:
/Pages
/Shared/
_Footer.cshtml
The content is very simple:
<h3> _Footer.cshtml partial view.</h3>
I also have a ASP.NET Core Web App project with same folder structure:
/Pages
_ViewImports.cshtml
-ViewStart.cshtml
/Shared
_Layout.cshtml
Somewhere in the layout file I make a reference to the partial view:
<partial name="_Footer" />
Here is the problem:
If I add a Project Reference of RCL to the Web App, the partial view is found and pages render fine.
If I make a Nuget package from the RCL, add it to a local Nuget source and add a reference to the package, the partial view won't be found.
The partial view '_Footer' was not found. The following locations were searched: /Pages/_Footer.cshtml /Pages/Shared/_Footer.cshtml /Views/Shared/_Footer.cshtml
What could be the difference between adding a project reference and Nuget reference? I've verified the Nuget package does contain both the Class Lib's default and views assemblies.
I found it works if you use dotnet pack instead of nuget pack. Note for dotnet pack package information is now read from csproj instead of nuget spec file.
In my case I wasn't even able to make it work in the scenario the op said it does work:
If I add a Project Reference of RCL to the Web App, the partial view
is found and pages render fine.
I was just getting the standard error message that the partial view was not found (even if the searched paths were correct).
After some head banging I solved this problem by adding the following xml block to both .csproj files:
RCL: e.g. RazorUIClassLib.csproj
Web App: e.g. WebApp1.csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
I had the same problem, the solved writing the full path from the library Area, for example:
In the library i have:
Areas/Footer/Pages/_footer.cshtml
with my UI Proyect i called it like: <partial name="~/Areas/Footer/Pages/_footer.cshtml" model="My Model instance"/>
I hope it can help you
According to the docs (https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk), I think you can use IncludeRazorContentInPack to include your .cshtml files in a NuGet package (it defaults to false).

Deleting PrecompiledViews.dll from ASP.Net Core 2 API

In .NET Core 2 Web API app, Publish to folder feature in MS VS 2017 produce:
<ProjectAssembly>.PrecompiledViews.dll
<ProjectAssembly>.PrecompiledViews.pdb
Offical docs says that PrecompiledViews related to precompiling Razor Views, but my API doesn't contain any views or static files, just REST endpoints that return json.
Using .Net reflector I found the PrecompiledViews.dll empty.
So I deleted PrecompiledViews.dll and tested my API and it seems to work fine without any warnings or exceptions.
Is it safe to delete PrecompiledViews.dll and pdp if the API not using any razor views? If yes, Is there option in VS 2017 to stop publishing unused PrecompiledViews?
You are right, the precompile step always emits an assembly and doesn't check if there are actually views. You can disable the precompilation step by putting this into your csproj file:
<PropertyGroup>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
This will then activate the normal copilation context preservation (refs subfolder). To deactivate this as well, add
<PreserveCompilationContext>false</PreserveCompilationContext>
to the property group.

Found conflicts between different versions of the same dependent assembly.MVC3 -> MVC4 / EF4 -> EF5

The question is how to resolve conflicts between versions of assemblies in my project that was upgraded to MVC4 and EF5?
The problem is manifest in the fact that my controllers and models can include System.Data.Objects, but now my views.
I am using MVC 4, my project was upgraded from MVC 3.
Entity Framework is version 5.
I have a controller that is able to use objectcontext from System.Data.Objects.
My Usings:
using System.Data.Objects;
using System.Data.Entity;
When I try to include the using in the view form System.Data.Objects, I get :
CS0234: The type or namespace name 'Objects' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
I am targeting .net 4.5
My Build Displays this message:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1561,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly.
You can build your solution in diagnostic mode to get more detailed information about the error.
Open the VS Options dialog (Tools > Options), navigate to the "Projects and Solutions" node and select "Build and Run". Change the MS Build project build output verbosity to Diagnostic.
Have a look here.
If you look at the build message, it states the 4.0 version of the .net framework is referenced... Is there a setting in your project file or web/app.config specifying a conflicting version of the .net framework?
Are you familiar with fuslog? you can set it up to log all assembly bindings that .net is doing while running your application. You should then be able to see detailed information on what is getting bound when. If you still can't figure it out, you can always do a binding redirect on that .dll in the web.config.
http://msdn.microsoft.com/en-us/library/eftw1fys.aspx -- binding redirects
http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.71).aspx -- fusion log viewer
Set up fusion logger and take a look at what the output is. If you don't get an answer from that, try the binding redirect (which would give you at least a temporary solution).
In the directory I was publishing to, there was a folder named aspnet_client. I moved it (instead of deleting it), republished, and it worked. I'm not sure why that folder decided to give me trouble out of the blue.

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