How can I see the code generated for the Razor page when using Asp.Net Core?

With Asp.Net, it was easy to see the code generated by the Razor View Engine: Add a compilation error, and the error page would provide access to the source of the Razor page.

This has changed with the help of the Asp.Net Core, which I read somewhere, creating code in memory and not allowing easy access to this code.

Question: Does anyone know how to access the generated Razor source code from Asp.Net Core?

+6
source share
3 answers

Add the following class to your ASP.NET Core MVC project:

using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

public class CustomCompilationService : DefaultRoslynCompilationService, ICompilationService
{
    public CustomCompilationService(ApplicationPartManager partManager, 
        IOptions<RazorViewEngineOptions> optionsAccessor, 
        IRazorViewEngineFileProviderAccessor fileProviderAccessor, 
        ILoggerFactory loggerFactory) 
        : base(partManager, optionsAccessor, fileProviderAccessor, loggerFactory)
    {
    }

    CompilationResult ICompilationService.Compile(RelativeFileInfo fileInfo, 
        string compilationContent)
    {
        return base.Compile(fileInfo, compilationContent);
    }
}

Override ICompilationService, added MVC with the above class;

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton<ICompilationService, CustomCompilationService>();
}

Compile CustomCompilationService compilationContent.

enter image description here

. Index (Index.cshtml), Index (Index.cshtml), :

InvalidOperationException: "Index" .

+6

ASP.NET 1.x. 2.0 :

using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;

public class CustomMvcRazorTemplateEngine : MvcRazorTemplateEngine
{
  public CustomMvcRazorTemplateEngine(RazorEngine engine, RazorProject project) : base(engine, project)
  { }

  public override RazorCSharpDocument GenerateCode(RazorCodeDocument codeDocument)
  {
    RazorCSharpDocument razorCSharpDocument = base.GenerateCode(codeDocument);
    // Set breakpoint here for inspecting the generated C# code in razorCSharpDocument.GeneratedCode
    // The razor code can be inspected in the Autos or Locals window in codeDocument.Source._innerSourceDocument._content 
    return razorCSharpDocument;
  }
}

RazorTemplateEngine :

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddSingleton<RazorTemplateEngine, CustomMvcRazorTemplateEngine>();
}
+4

:

using Microsoft.AspNetCore.Razor.Language;

class Program
{
    static void Main(string[] args)
    {
        var sourceDocument = RazorSourceDocument.Create("Hello world", "");
        var codeDocument = RazorCodeDocument.Create(sourceDocument);

        var engine = RazorEngine.Create();
        engine.Process(codeDocument);

        var csharpDocument = codeDocument.GetCSharpDocument();
        var csharp = csharpDocument.GeneratedCode;
        Console.WriteLine(csharp);
    }
}

:

#pragma checksum "" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7b502c3a1f48c8609ae212cdfb639dee39673f5e"
// <auto-generated/>
#pragma warning disable 1591
namespace Razor
{
    #line hidden
    public class Template
    {
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            WriteLiteral("Hello world");
        }
        #pragma warning restore 1998
    }
}
#pragma warning restore 1591
+3

Source: https://habr.com/ru/post/1016010/


All Articles