Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- services.Configure<RazorViewEngineOptions>(o => {
- o.ViewLocationFormats.Add("/{0}" + RazorViewEngine.ViewExtension);
- o.FileProviders.Clear();
- o.FileProviders.Add(new PhysicalFileProvider(AppContext.BaseDirectory));
- });
- <ItemGroup>
- <Content Include="Documents***.cshtml">
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
- </Content>
- </ItemGroup>
- public async Task<string> RenderAsync<TModel>(string name, TModel model) {
- var actionContext = GetDefaultActionContext();
- var viewEngineResult = _viewEngine.GetView(null, name, true);
- if (!viewEngineResult.Success) {
- throw new InvalidOperationException($"View '{name}' cannot be found.");
- }
- 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();
- }
- }
Add Comment
Please, Sign In to add comment