J'aimerais utiliser Razor en tant que moteur de modélisation dans une application console .NET que j'écris dans .NET Core.
Les moteurs Razor autonomes que j'ai rencontrés (RazorEngine, RazorTemplates) requièrent tous un .NET complet. Je recherche une solution qui fonctionne avec .NET Core.
Récemment, j'ai créé une bibliothèque appelée RazorLight .
Il n'a pas de dépendances redondantes, comme les composants ASP.NET MVC, et peut être utilisé dans les applications console. Pour l'instant, il ne prend en charge que .NET Core (NetStandard1.6) - mais c'est exactement ce dont vous avez besoin.
Voici un court exemple:
IRazorLightEngine engine = EngineFactory.CreatePhysical("Path-to-your-views");
// Files and strong models
string resultFromFile = engine.Parse("Test.cshtml", new Model("SomeData"));
// Strings and anonymous models
string stringResult = engine.ParseString("Hello @Model.Name", new { Name = "John" });
Voici un exemple de code qui ne dépend que de Razor (pour l'analyse et la génération de code C #) et de Roslyn (pour la compilation de code C #, mais vous pouvez également utiliser l'ancien CodeDom).
Il n'y a pas de code MVC dans ce morceau de code, donc, pas de vue, pas de fichiers .cshtml, pas de contrôleur, juste une analyse de source Razor et une exécution d'exécution compilée. Il y a toujours la notion de modèle cependant.
Vous aurez seulement besoin d’ajouter les paquets de nuget suivants: Microsoft.AspNetCore.Razor.Language
(v2.1.1), Microsoft.AspNetCore.Razor.Runtime
(v2.1.1) et Microsoft.CodeAnalysis.CSharp
(v2.8.2) pépites.
Ce code source C # est compatible avec NETCore, NETStandard 2 et .NET Framework. Pour le tester, créez simplement une application .NET Framework ou .NET Core Console, collez-le et ajoutez les nugets.
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace RazorTemplate
{
class Program
{
static void Main(string[] args)
{
// points to the local path
var fs = RazorProjectFileSystem.Create(".");
// customize the default engine a little bit
var engine = RazorProjectEngine.Create(RazorConfiguration.Default, fs, (builder) =>
{
InheritsDirective.Register(builder);
builder.SetNamespace("MyNamespace"); // define a namespace for the Template class
});
// get a razor-templated file. My "hello.txt" template file is defined like this:
//
// @inherits RazorTemplate.MyTemplate
// Hello @Model.Name, welcome to Razor World!
//
var item = fs.GetItem("hello.txt");
// parse and generate C# code, outputs it on the console
//var cs = te.GenerateCode(item);
//Console.WriteLine(cs.GeneratedCode);
var codeDocument = engine.Process(item);
var cs = codeDocument.GetCSharpDocument();
// now, use roslyn, parse the C# code
var tree = CSharpSyntaxTree.ParseText(cs.GeneratedCode);
// define the dll
const string dllName = "hello";
var compilation = CSharpCompilation.Create(dllName, new[] { tree },
new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location), // include corlib
MetadataReference.CreateFromFile(typeof(RazorCompiledItemAttribute).Assembly.Location), // include Microsoft.AspNetCore.Razor.Runtime
MetadataReference.CreateFromFile(Assembly.GetExecutingAssembly().Location), // this file (that contains the MyTemplate base class)
// for some reason on .NET core, I need to add this... this is not needed with .NET framework
MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "System.Runtime.dll")),
// as found out by @Isantipov, for some other reason on .NET Core for Mac and Linux, we need to add this... this is not needed with .NET framework
MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "netstandard.dll"))
},
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); // we want a dll
// compile the dll
string path = Path.Combine(Path.GetFullPath("."), dllName + ".dll");
var result = compilation.Emit(path);
if (!result.Success)
{
Console.WriteLine(string.Join(Environment.NewLine, result.Diagnostics));
return;
}
// load the built dll
Console.WriteLine(path);
var asm = Assembly.LoadFile(path);
// the generated type is defined in our custom namespace, as we asked. "Template" is the type name that razor uses by default.
var template = (MyTemplate)Activator.CreateInstance(asm.GetType("MyNamespace.Template"));
// run the code.
// should display "Hello Killroy, welcome to Razor World!"
template.ExecuteAsync().Wait();
}
}
// the model class. this is 100% specific to your context
public class MyModel
{
// this will map to @Model.Name
public string Name => "Killroy";
}
// the sample base template class. It's not mandatory but I think it's much easier.
public abstract class MyTemplate
{
// this will map to @Model (property name)
public MyModel Model => new MyModel();
public void WriteLiteral(string literal)
{
// replace that by a text writer for example
Console.Write(literal);
}
public void Write(object obj)
{
// replace that by a text writer for example
Console.Write(obj);
}
public async virtual Task ExecuteAsync()
{
await Task.Yield(); // whatever, we just need something that compiles...
}
}
}
Il existe un exemple de travail pour .NET Core 1.0 à l’adresse aspnet/Entropy/samples/Mvc.RenderViewToString . Comme cela pourrait changer ou disparaître, je vais détailler l'approche que j'utilise dans mes propres applications ici.
Tl; dr - Razor fonctionne vraiment bien en dehors de MVC! Cette approche peut gérer des scénarios de rendu plus complexes tels que des vues partielles et l'injection d'objets dans des vues, bien que je ne montre qu'un exemple simple ci-dessous.
Le service de base ressemble à ceci:
RazorViewToStringRenderer.cs
using System;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
namespace RenderRazorToString
{
public class RazorViewToStringRenderer
{
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public RazorViewToStringRenderer(
IRazorViewEngine viewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public async Task<string> RenderViewToString<TModel>(string name, TModel model)
{
var actionContext = GetActionContext();
var viewEngineResult = _viewEngine.FindView(actionContext, name, false);
if (!viewEngineResult.Success)
{
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
}
var view = viewEngineResult.View;
using (var output = new StringWriter())
{
var viewContext = new ViewContext(
actionContext,
view,
new ViewDataDictionary<TModel>(
metadataProvider: new EmptyModelMetadataProvider(),
modelState: new ModelStateDictionary())
{
Model = model
},
new TempDataDictionary(
actionContext.HttpContext,
_tempDataProvider),
output,
new HtmlHelperOptions());
await view.RenderAsync(viewContext);
return output.ToString();
}
}
private ActionContext GetActionContext()
{
var httpContext = new DefaultHttpContext
{
RequestServices = _serviceProvider
};
return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
}
}
}
Une application de console de test simple doit simplement initialiser le service (et certains services de support) et l'appeler:
Program.cs
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;
namespace RenderRazorToString
{
public class Program
{
public static void Main()
{
// Initialize the necessary services
var services = new ServiceCollection();
ConfigureDefaultServices(services);
var provider = services.BuildServiceProvider();
var renderer = provider.GetRequiredService<RazorViewToStringRenderer>();
// Build a model and render a view
var model = new EmailViewModel
{
UserName = "User",
SenderName = "Sender"
};
var emailContent = renderer.RenderViewToString("EmailTemplate", model).GetAwaiter().GetResult();
Console.WriteLine(emailContent);
Console.ReadLine();
}
private static void ConfigureDefaultServices(IServiceCollection services)
{
var applicationEnvironment = PlatformServices.Default.Application;
services.AddSingleton(applicationEnvironment);
var appDirectory = Directory.GetCurrentDirectory();
var environment = new HostingEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(appDirectory),
ApplicationName = "RenderRazorToString"
};
services.AddSingleton<IHostingEnvironment>(environment);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddLogging();
services.AddMvc();
services.AddSingleton<RazorViewToStringRenderer>();
}
}
}
Cela suppose que vous ayez une classe de modèle de vue:
EmailViewModel.cs
namespace RenderRazorToString
{
public class EmailViewModel
{
public string UserName { get; set; }
public string SenderName { get; set; }
}
}
Et la mise en page et voir les fichiers:
Vues/_Layout.cshtml
<!DOCTYPE html>
<html>
<body>
<div>
@RenderBody()
</div>
<footer>
Thanks,<br />
@Model.SenderName
</footer>
</body>
</html>
Vues/EmailTemplate.cshtml
@model RenderRazorToString.EmailViewModel
@{
Layout = "_EmailLayout";
}
Hello @Model.UserName,
<p>
This is a generic email about something.<br />
<br />
</p>
Voici une classe pour que la réponse de Nate fonctionne comme un service de portée dans un projet ASP.NET Core 2.0.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
namespace YourNamespace.Services
{
public class ViewRender : IViewRender
{
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public ViewRender(
IRazorViewEngine viewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public async Task<string> RenderAsync(string name)
{
return await RenderAsync<object>(name, null);
}
public async Task<string> RenderAsync<TModel>(string name, TModel model)
{
var actionContext = GetActionContext();
var viewEngineResult = _viewEngine.FindView(actionContext, name, false);
if (!viewEngineResult.Success)
{
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
}
var view = viewEngineResult.View;
using (var output = new StringWriter())
{
var viewContext = new ViewContext(
actionContext,
view,
new ViewDataDictionary<TModel>(
metadataProvider: new EmptyModelMetadataProvider(),
modelState: new ModelStateDictionary())
{
Model = model
},
new TempDataDictionary(
actionContext.HttpContext,
_tempDataProvider),
output,
new HtmlHelperOptions());
await view.RenderAsync(viewContext);
return output.ToString();
}
}
private ActionContext GetActionContext()
{
var httpContext = new DefaultHttpContext {RequestServices = _serviceProvider};
return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
}
}
public interface IViewRender
{
Task<string> RenderAsync(string name);
Task<string> RenderAsync<TModel>(string name, TModel model);
}
}
Dans Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IViewRender, ViewRender>();
}
Dans un contrôleur
public class VenuesController : Controller
{
private readonly IViewRender _viewRender;
public VenuesController(IViewRender viewRender)
{
_viewRender = viewRender;
}
public async Task<IActionResult> Edit()
{
string html = await _viewRender.RenderAsync("Emails/VenuePublished", venue.Name);
return Ok();
}
}
J'ai passé plusieurs jours à manipuler la lumière rasoir, mais elle présente un certain nombre d'inconvénients: ne pas avoir d'auxiliaires HTML (@ Html. *), D'auxiliaires URL et d'autres bizarreries.
Voici une solution qui est encapsulée pour une utilisation en dehors d’une application MVC. Il nécessite des références de paquetages pour aspnet core et mvc, mais il est facile de les ajouter à un service ou à une application console. Aucun contrôleur ou serveur Web n'est nécessaire. RenderToStringAsync est la méthode à appeler pour afficher une vue en chaîne.
L’avantage est que vous pouvez rédiger vos vues de la même manière que dans un projet Web central .net. Vous pouvez utiliser le même @Html et d'autres fonctions et méthodes d'assistance.
Vous pouvez remplacer ou ajouter des fournisseurs de fichiers physiques dans la configuration des options de vue rasoir par votre propre fournisseur personnalisé pour charger des vues à partir d'une base de données, d'un appel de service Web, etc. Testé avec .net core 2.2 sous Windows et Linux.
Veuillez noter que votre fichier .csproj doit avoir ceci en haut de la ligne:
<Project Sdk="Microsoft.NET.Sdk.Web">
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
namespace RazorRendererNamespace
{
/// <summary>
/// Renders razor pages with the absolute minimum setup of MVC, easy to use in console application, does not require any other classes or setup.
/// </summary>
public class RazorRenderer : ILoggerFactory, ILogger
{
private class ViewRenderService : IDisposable, ITempDataProvider, IServiceProvider
{
private static readonly System.Net.IPAddress localIPAddress = System.Net.IPAddress.Parse("127.0.0.1");
private readonly Dictionary<string, object> tempData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
private readonly IHttpContextAccessor _httpContextAccessor;
public ViewRenderService(IRazorViewEngine viewEngine,
IHttpContextAccessor httpContextAccessor,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_httpContextAccessor = httpContextAccessor;
_tempDataProvider = tempDataProvider ?? this;
_serviceProvider = serviceProvider ?? this;
}
public void Dispose()
{
}
public async Task<string> RenderToStringAsync<TModel>(string viewName, TModel model, ExpandoObject viewBag = null, bool isMainPage = false)
{
HttpContext httpContext;
if (_httpContextAccessor?.HttpContext != null)
{
httpContext = _httpContextAccessor.HttpContext;
}
else
{
DefaultHttpContext defaultContext = new DefaultHttpContext { RequestServices = _serviceProvider };
defaultContext.Connection.RemoteIpAddress = localIPAddress;
httpContext = defaultContext;
}
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _viewEngine.FindView(actionContext, viewName, isMainPage);
if (viewResult.View == null)
{
viewResult = _viewEngine.GetView("~/", viewName, isMainPage);
}
if (viewResult.View == null)
{
return null;
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
if (viewBag != null)
{
foreach (KeyValuePair<string, object> kv in (viewBag as IDictionary<string, object>))
{
viewDictionary.Add(kv.Key, kv.Value);
}
}
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
object IServiceProvider.GetService(Type serviceType)
{
return null;
}
IDictionary<string, object> ITempDataProvider.LoadTempData(HttpContext context)
{
return tempData;
}
void ITempDataProvider.SaveTempData(HttpContext context, IDictionary<string, object> values)
{
}
}
private readonly string rootPath;
private readonly ServiceCollection services;
private readonly ServiceProvider serviceProvider;
private readonly ViewRenderService viewRenderer;
public RazorRenderer(string rootPath)
{
this.rootPath = rootPath;
services = new ServiceCollection();
ConfigureDefaultServices(services);
serviceProvider = services.BuildServiceProvider();
viewRenderer = new ViewRenderService(serviceProvider.GetRequiredService<IRazorViewEngine>(), null, null, serviceProvider);
}
private void ConfigureDefaultServices(IServiceCollection services)
{
var environment = new HostingEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(rootPath),
ApplicationName = typeof(RazorRenderer).Assembly.GetName().Name,
ContentRootPath = rootPath,
WebRootPath = rootPath,
EnvironmentName = "DEVELOPMENT",
ContentRootFileProvider = new PhysicalFileProvider(rootPath)
};
services.AddSingleton<IHostingEnvironment>(environment);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(rootPath));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddSingleton<ILoggerFactory>(this);
var diagnosticSource = new DiagnosticListener(environment.ApplicationName);
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddMvc();
}
public void Dispose()
{
}
public Task<string> RenderToStringAsync<TModel>(string viewName, TModel model, ExpandoObject viewBag = null, bool isMainPage = false)
{
return viewRenderer.RenderToStringAsync(viewName, model, viewBag, isMainPage);
}
void ILoggerFactory.AddProvider(ILoggerProvider provider)
{
}
IDisposable ILogger.BeginScope<TState>(TState state)
{
throw new NotImplementedException();
}
ILogger ILoggerFactory.CreateLogger(string categoryName)
{
return this;
}
bool ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
{
return false;
}
void ILogger.Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
}
}
}