razor

https://cloud.tencent.com/developer/ask/59115

https://blog.csdn.net/megoodtoo/article/details/77160561

IRazorLightEngine engine = EngineFactory.CreatePhysical("Path-to-your-views");

// Files and strong models
string resultFromFile = engine.Parse("Test.cshtml", new Model("SomeData")); 

https://weblog.west-wind.com/posts/2010/dec/27/hosting-the-razor-engine-for-templating-in-nonweb-applications

https://andrewlock.net/configuring-environment-specific-services-in-asp-net-core/

https://inneka.com/programming/c/return-view-as-string-in-net-core/

https://medium.com/a-layman/refactor-your-html-report-with-razorengine-59e878ac9d52

https://stackoverflow.com/questions/40912375/return-view-as-string-in-net-core

https://ppolyzos.com/2016/09/09/asp-net-core-render-view-to-string/

 https://ppolyzos.com/2016/09/09/asp-net-core-render-view-to-string/

https://long2know.com/2017/08/rendering-and-emailing-embedded-razor-views-with-net-core/

https://medium.com/a-layman/refactor-your-html-report-with-razorengine-59e878ac9d52

using System.Diagnostics;
  using System.IO;
  using System.Reflection;
  using System.Threading.Tasks;
  using ClassLibrary;
  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 Console
  {
  class Program
  {
  static async Task Main(string[] args)
  {
  var renderer = GetRenderer();
  var html = await renderer.RenderViewToStringAsync("/Myview.cshtml", new MyModel());
  System.Console.Write(html);
  System.Console.ReadKey();
  }
   
  private static RazorViewToStringRenderer GetRenderer()
  {
  var services = new ServiceCollection();
  var applicationEnvironment = PlatformServices.Default.Application;
  services.AddSingleton(applicationEnvironment);
   
  var appDirectory = Directory.GetCurrentDirectory();
   
  var environment = new HostingEnvironment
  {
  ApplicationName = Assembly.GetEntryAssembly().GetName().Name
  };
  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>();
  var provider = services.BuildServiceProvider();
  return provider.GetRequiredService<RazorViewToStringRenderer>();
  }
  }
  }
// Copyright (c) .NET Foundation. All rights reserved.
  // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
   
  using System;
  using System.IO;
  using System.Linq;
  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.ViewEngines;
  using Microsoft.AspNetCore.Mvc.ViewFeatures;
  using Microsoft.AspNetCore.Routing;
   
  namespace Mvc.RenderViewToString
  {
  public class RazorViewToStringRenderer
  {
  private IRazorViewEngine _viewEngine;
  private ITempDataProvider _tempDataProvider;
  private IServiceProvider _serviceProvider;
   
  public RazorViewToStringRenderer(
  IRazorViewEngine viewEngine,
  ITempDataProvider tempDataProvider,
  IServiceProvider serviceProvider)
  {
  _viewEngine = viewEngine;
  _tempDataProvider = tempDataProvider;
  _serviceProvider = serviceProvider;
  }
   
  public async Task<string> RenderViewToStringAsync<TModel>(string viewName, TModel model)
  {
  var actionContext = GetActionContext();
  var view = FindView(actionContext, viewName);
   
  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 IView FindView(ActionContext actionContext, string viewName)
  {
  var getViewResult = _viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
  if (getViewResult.Success)
  {
  return getViewResult.View;
  }
   
  var findViewResult = _viewEngine.FindView(actionContext, viewName, isMainPage: true);
  if (findViewResult.Success)
  {
  return findViewResult.View;
  }
   
  var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
  var errorMessage = string.Join(
  Environment.NewLine,
  new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations)); ;
   
  throw new InvalidOperationException(errorMessage);
  }
   
  private ActionContext GetActionContext()
  {
  var httpContext = new DefaultHttpContext();
  httpContext.RequestServices = _serviceProvider;
  return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
  }
  }
  }
原文地址:https://www.cnblogs.com/zwei1121/p/12017060.html