Il existe un exemple fonctionnel pour .NET Core 1.0 à l'adresse suivante aspnet/Entropy/samples/Mvc.RenderViewToString . Comme cela pourrait changer ou disparaître, je vais détailler ici l'approche que j'utilise dans mes propres applications.
Tl;dr - Razor fonctionne très bien en dehors de MVC ! Cette approche permet de gérer des scénarios de rendu plus complexes, comme les vues partielles et l'injection d'objets dans les vues, mais je vais me contenter d'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 simple application de console de test doit simplement initialiser le service (et certains services de soutien), et l'appeler :
Programme.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 avez 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 les fichiers de mise en page et de visualisation :
Vues/_Layout.cshtml
<!DOCTYPE html>
<html>
<body>
<div>
@RenderBody()
</div>
<footer>
Thanks,<br />
@Model.SenderName
</footer>
</body>
</html>
Views/EmailTemplate.cshtml
@model RenderRazorToString.EmailViewModel
@{
Layout = "_EmailLayout";
}
Hello @Model.UserName,
<p>
This is a generic email about something.<br />
<br />
</p>
2 votes
github.com/aspnet/Razor ne nécessite que le runtime de base (utilisant la bibliothèque standard .NET)