RSCG – RazorSlices
RSCG – RazorSlices
name | RazorSlices |
nuget | https://www.nuget.org/packages/RazorSlices/ |
link | https://github.com/DamianEdwards/RazorSlices |
author | Damiam Edwards |
Generating html from razor templates. Attention: generates IHttpResult, not html string.
This is how you can use RazorSlices .
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | < Project Sdk = "Microsoft.NET.Sdk.Web" > < PropertyGroup > < TargetFramework >net8.0</ TargetFramework > < Nullable >enable</ Nullable > < ImplicitUsings >enable</ ImplicitUsings > </ PropertyGroup > < ItemGroup > < PackageReference Include = "Microsoft.AspNetCore.OpenApi" Version = "8.0.10" /> < PackageReference Include = "RazorSlices" Version = "0.8.1" /> < PackageReference Include = "Swashbuckle.AspNetCore" Version = "6.6.2" /> </ ItemGroup > < PropertyGroup > < EmitCompilerGeneratedFiles >true</ EmitCompilerGeneratedFiles > < CompilerGeneratedFilesOutputPath >$(BaseIntermediateOutputPath)\GX</ CompilerGeneratedFilesOutputPath > </ PropertyGroup > </ Project > |
The code that you will use is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using RazorDemoSlices; var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } var summaries = new [] { "Freezing" , "Bracing" , "Chilly" , "Cool" , "Mild" , "Warm" , "Balmy" , "Hot" , "Sweltering" , "Scorching" }; app.MapGet( "/hello" , ( string firstName, string lastName) => Results.Extensions.RazorSlice<RazorDemoSlices.Slices.PersonHTML, Person>( new Person { FirstName = firstName, LastName = lastName } )); app.MapGet( "/weatherforecast" , () => { var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast ( DateOnly.FromDateTime(DateTime.Now.AddDays(index)), Random.Shared.Next(-20, 55), summaries[Random.Shared.Next(summaries.Length)] )) .ToArray(); return forecast; }) .WithName( "GetWeatherForecast" ) .WithOpenApi(); app.Run(); internal record WeatherForecast(DateOnly Date, int TemperatureC, string ? Summary) { public int TemperatureF => 32 + ( int )(TemperatureC / 0.5556); } |
01 02 03 04 05 06 07 08 09 10 11 12 13 | @inherits RazorSliceHttpResult<RazorDemoSlices.Person> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>Hello from Razor Slices!</title> </head> <body> <p> My name is @Model.FirstName @Model.LastName </p> </body> </html> |
1 2 3 4 5 6 7 | namespace RazorDemoSlices; public class Person { public string FirstName { get ; set ; }= string .Empty; public string LastName { get ; set ; }= string .Empty; } |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #pragma checksum "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\PersonHTML.cshtml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "6b3710e80836b438a5d8935ea469d238fc095e46298456ca847e09519393bb5e" // <auto-generated/> #pragma warning disable 1591 [assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute( typeof (AspNetCoreGeneratedDocument.Slices_PersonHTML), @"mvc.1.0.view" , @"/Slices/PersonHTML.cshtml" )] namespace AspNetCoreGeneratedDocument { #line default using global::System; using global::System.Collections.Generic; using global::System.Linq; using global::System.Threading.Tasks; using global::Microsoft.AspNetCore.Mvc; using global::Microsoft.AspNetCore.Mvc.Rendering; using global::Microsoft.AspNetCore.Mvc.ViewFeatures; #nullable restore #line (3,2)-(4,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using System.Globalization; #nullable disable #nullable restore #line (4,2)-(5,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using Microsoft.AspNetCore.Razor; #nullable disable #nullable restore #line (5,2)-(6,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using Microsoft.AspNetCore.Http.HttpResults; #line default #line hidden #nullable disable [global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute( "Identifier" , "/Slices/PersonHTML.cshtml" )] [global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute] #nullable restore internal sealed class Slices_PersonHTML : RazorSliceHttpResult<RazorDemoSlices.Person> #nullable disable { #pragma warning disable 1998 public async override global::System.Threading.Tasks.Task ExecuteAsync() { WriteLiteral( "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"utf-8\">\r\n <title>Hello from Razor Slices!</title>\r\n</head>\r\n<body>\r\n <p>\r\n My name is " ); Write( #nullable restore #line (10,21)-(10,36) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\PersonHTML.cshtml" Model.FirstName #line default #line hidden #nullable disable ); WriteLiteral( " " ); Write( #nullable restore #line (10,38)-(10,52) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\PersonHTML.cshtml" Model.LastName #line default #line hidden #nullable disable ); WriteLiteral( "\r\n </p>\r\n</body>\r\n</html>" ); } #pragma warning restore 1998 #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get ; private set ; } = default !; #nullable disable } } #pragma warning restore 1591 |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #pragma checksum "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "95493514af34e5705fffb1e5c7121f0e7abde13ee7a1cff8fbafa2085da18fff" // <auto-generated/> #pragma warning disable 1591 [assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute( typeof (AspNetCoreGeneratedDocument.Slices__ViewImports), @"mvc.1.0.view" , @"/Slices/_ViewImports.cshtml" )] namespace AspNetCoreGeneratedDocument { #line default using global::System; using global::System.Collections.Generic; using global::System.Linq; using global::System.Threading.Tasks; using global::Microsoft.AspNetCore.Mvc; using global::Microsoft.AspNetCore.Mvc.Rendering; using global::Microsoft.AspNetCore.Mvc.ViewFeatures; #nullable restore #line (3,2)-(4,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using System.Globalization; #nullable disable #nullable restore #line (4,2)-(5,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using Microsoft.AspNetCore.Razor; #nullable disable #nullable restore #line (5,2)-(6,1) "D:\gth\RSCG_Examples\v2\rscg_examples\EmbeddedResourcePropertyGenerator\src\RazorDemoSlices\RazorDemoSlices\Slices\_ViewImports.cshtml" using Microsoft.AspNetCore.Http.HttpResults; #line default #line hidden #nullable disable [global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute( "Identifier" , "/Slices/_ViewImports.cshtml" )] [global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute] #nullable restore internal sealed class Slices__ViewImports : RazorSliceHttpResult #nullable disable { #pragma warning disable 1998 public async override global::System.Threading.Tasks.Task ExecuteAsync() { WriteLiteral( "\r\n" ); WriteLiteral( "\r\n" ); } #pragma warning restore 1998 #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get ; private set ; } = default !; #nullable disable #nullable restore [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get ; private set ; } = default !; #nullable disable } } #pragma warning restore 1591 |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // <auto-generated/> using global::System.Diagnostics.CodeAnalysis; using global::RazorSlices; #nullable enable namespace RazorDemoSlices { /// <summary> /// All calls to create Razor Slices instances via the generated <see cref="global::RazorSlices.IRazorSliceProxy"/> classes /// go through this factory to ensure that the generated types' Create methods are always invoked via the static abstract /// methods defined in the <see cref="global::RazorSlices.IRazorSliceProxy"/> interface. This ensures that the interface /// implementation is never trimmed from the generated types. /// </summary> /// <remarks> /// Workaround for https://github.com/dotnet/runtime/issues/102796 /// </remarks> [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] // Hide from IntelliSense. internal static class RazorSlicesGenericFactory { public static RazorSlice CreateSlice<TProxy>() where TProxy : IRazorSliceProxy => TProxy.CreateSlice(); public static RazorSlice<TModel> CreateSlice<TProxy, TModel>(TModel model) where TProxy : IRazorSliceProxy => TProxy.CreateSlice(model); } } namespace RazorDemoSlices.Slices { /// <summary> /// Static proxy for the Razor Slice defined in <c>Slices\PersonHTML.cshtml</c>. /// </summary> public sealed class PersonHTML : global::RazorSlices.IRazorSliceProxy { [global::System.Diagnostics.CodeAnalysis.DynamicDependency(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, TypeName, "RazorDemoSlices" )] private const string TypeName = "AspNetCoreGeneratedDocument.Slices_PersonHTML, RazorDemoSlices" ; [global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] private static readonly global::System.Type _sliceType = global::System.Type.GetType(TypeName) ?? throw new global::System.InvalidOperationException($ "Razor view type '{TypeName}' was not found. This is likely a bug in the RazorSlices source generator." ); private static readonly global::RazorSlices.SliceDefinition _sliceDefinition = new (_sliceType); /// <summary> /// Creates a new instance of the Razor Slice defined in <c>Slices\PersonHTML.cshtml</c> . /// </summary> public static global::RazorSlices.RazorSlice Create() => global::RazorDemoSlices.RazorSlicesGenericFactory.CreateSlice<global::RazorDemoSlices.Slices.PersonHTML>(); /// <summary> /// Creates a new instance of the Razor Slice defined in <c>Slices\PersonHTML.cshtml</c> with the given model. /// </summary> public static global::RazorSlices.RazorSlice<TModel> Create<TModel>(TModel model) => global::RazorDemoSlices.RazorSlicesGenericFactory.CreateSlice<global::RazorDemoSlices.Slices.PersonHTML, TModel>(model); // Explicit interface implementation static global::RazorSlices.RazorSlice global::RazorSlices.IRazorSliceProxy.CreateSlice() => _sliceDefinition.CreateSlice(); // Explicit interface implementation static global::RazorSlices.RazorSlice<TModel> global::RazorSlices.IRazorSliceProxy.CreateSlice<TModel>(TModel model) => _sliceDefinition.CreateSlice(model); } } |
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/RazorSlices
Leave a Reply