daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Diagnostics;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using ClassLibrary;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Hosting.Internal;
  8. using Microsoft.AspNetCore.Mvc.Razor;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.FileProviders;
  11. using Microsoft.Extensions.ObjectPool;
  12. using Microsoft.Extensions.PlatformAbstractions;
  13.  
  14. namespace Console
  15. {
  16.     class Program
  17.     {
  18.         static async Task Main(string[] args)
  19.         {
  20.             var renderer = GetRenderer();
  21.             var html = await renderer.RenderViewToStringAsync("/Myview.cshtml", new MyModel());
  22.             System.Console.Write(html);
  23.             System.Console.ReadKey();
  24.         }
  25.  
  26.         private static RazorViewToStringRenderer GetRenderer()
  27.         {
  28.             var services = new ServiceCollection();
  29.             var applicationEnvironment = PlatformServices.Default.Application;
  30.             services.AddSingleton(applicationEnvironment);
  31.  
  32.             var appDirectory = Directory.GetCurrentDirectory();
  33.  
  34.             var environment = new HostingEnvironment
  35.             {
  36.                 ApplicationName = Assembly.GetEntryAssembly().GetName().Name
  37.             };
  38.             services.AddSingleton<IHostingEnvironment>(environment);
  39.  
  40.             services.Configure<RazorViewEngineOptions>(options =>
  41.             {
  42.                 options.FileProviders.Clear();
  43.                 options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
  44.             });
  45.  
  46.             services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
  47.  
  48.             var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
  49.             services.AddSingleton<DiagnosticSource>(diagnosticSource);
  50.  
  51.             services.AddLogging();
  52.             services.AddMvc();
  53.             services.AddSingleton<RazorViewToStringRenderer>();
  54.             var provider = services.BuildServiceProvider();
  55.             return provider.GetRequiredService<RazorViewToStringRenderer>();
  56.         }
  57.     }
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top