Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. services.Configure<RazorViewEngineOptions>(o => {
  2. o.ViewLocationFormats.Add("/{0}" + RazorViewEngine.ViewExtension);
  3. o.FileProviders.Clear();
  4. o.FileProviders.Add(new PhysicalFileProvider(AppContext.BaseDirectory));
  5. });
  6.  
  7. <ItemGroup>
  8. <Content Include="Documents***.cshtml">
  9. <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  10. <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  11. </Content>
  12. </ItemGroup>
  13.  
  14. public async Task<string> RenderAsync<TModel>(string name, TModel model) {
  15. var actionContext = GetDefaultActionContext();
  16. var viewEngineResult = _viewEngine.GetView(null, name, true);
  17.  
  18. if (!viewEngineResult.Success) {
  19. throw new InvalidOperationException($"View '{name}' cannot be found.");
  20. }
  21.  
  22. var view = viewEngineResult.View;
  23. using (var output = new StringWriter()) {
  24. var viewContext = new ViewContext(actionContext, view,
  25. new ViewDataDictionary<TModel>(
  26. metadataProvider: new EmptyModelMetadataProvider(),
  27. modelState: new ModelStateDictionary()) {
  28. Model = model
  29. },
  30. new TempDataDictionary(
  31. actionContext.HttpContext,
  32. _tempDataProvider),
  33. output,
  34. new HtmlHelperOptions());
  35.  
  36. await view.RenderAsync(viewContext);
  37. return output.ToString();
  38. }
  39. }
Add Comment
Please, Sign In to add comment