Closed pkrukp closed 3 years ago
Cool idea.
I'm a bit confused as to what I am looking at. Is Index.cshtml.razorlight.cs.txt generated via RazorSourceGenerator.GenerateCodeAsync
?
There are going to be significant differences between your regular Razor files and RazorLight, as you can see. There is magic around the razor template page base class and how the actual template gets materialized.
Is Index.cshtml.razorlight.cs.txt generated via RazorSourceGenerator.GenerateCodeAsync?
Yes, I just added code to dump generated code to a file in that function, around line 80:
RazorCSharpDocument document = codeDocument.GetCSharpDocument();
var tempFile = Path.Combine("C:/tmp/generated", Path.GetRandomFileName() + ".cs"); // added
File.WriteAllText(tempFile, document.GeneratedCode); // added
There is magic around the razor template page base class and how the actual template gets materialized.
Does it mean that in general it's not possible to generate code for actual .cshtml/.razor files that are used in web applications?
Does it mean that in general it's not possible to generate code for actual .cshtml/.razor files that are used in web applications?
For your use case, you should not use RazorLight. You can use RazorLight for some inspiration on how to call the razor engine, but you're really misapplying the library and building on quicksand. You are better off creating a separate project. Have you seen if anyone else has built any Razor analyzers for .NET Core? I think there were some for ASP.NET MVC Classic razor, but believe those went unmaintained when they rewrote the Razor engine for .NET Core. I believe Andrew Peters was the mind behind the rewrite, but IIRC he no longer works at MSFT and is on to other things.
Thanks for information.
I haven't seen any standalone Razor analyzers for asp.net core. It should be possible to write such analyzer for example for Visual Studio, where VS handles compilation and provides you Roslyn parse tree. Although I don't know how would it work if the razor file was excluded from compilation...
Hi,
I would like to generate .cs from .cshtml files. I don't need/want to render them.
RazorSourceGenerator.GenerateCodeAsync
, what would be the best way to use it?dotnet new mvc
and took a look at RazorLight-generated .cs file for Index.cshtml and compared it with .g.cs generated duringdotnet build
. They are a bit different (looks like MVC part is missing from RazorLight generated file? and also other differences line namespaces, path to file etc). Is there a way to make them identical?I'm attaching original and generated files for your convenience.
For the background story, I'd like to use Roslyn to analyze .cshtml/.razor files. For this I need to generated .cs files from them. In most cases .cs files are generated during build into e.g. obj/Debug/netcoreapp3.1/Razor/*.g.cs. But some web applications don't generate/compile them during build, so I need to do it myself.
Index.cshtml.txt Index.cshtml.g.cs.txt Index.cshtml.razorlight.cs.txt