我需要将Razor页面部署为字符串。
我想创建一个控制器操作,该操作使用包含部分视图和其他可选参数的JSON进行响应。
我熟悉以下将视图呈现为字符串的示例:https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs
然而,它与Pages不兼容,因为它只在Views目录中搜索,所以即使我给它一个绝对路径,它试图找到我的_Layout.cshtml(它甚至不应该这样做!) )并没有找到它。
我试图修改它以便渲染页面,但在尝试渲染时,我最终在部分中获得了ViewData的NullReferenceException。我怀疑它与NullView有关,但我不知道该放在那里(RazorView的构造函数需要许多我不知道如何正确获取的对象)。
代码:
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
// Modified by OronDF343: Uses pages instead of views.
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.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.AspNetCore.Routing;
namespace TestAspNetCore.Services
{
public class RazorPageToStringRenderer
{
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public RazorPageToStringRenderer(
IRazorViewEngine viewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public async Task<string> RenderPageToStringAsync<TModel>(string viewName, TModel model)
{
var actionContext = GetActionContext();
var page = FindPage(actionContext, viewName);
using (var output = new StringWriter())
{
var viewContext = new ViewContext(actionContext,
new NullView(),
new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(),
new ModelStateDictionary())
{
Model = model
},
new TempDataDictionary(actionContext.HttpContext,
_tempDataProvider),
output,
new HtmlHelperOptions());
page.ViewContext = viewContext;
await page.ExecuteAsync();
return output.ToString();
}
}
private IRazorPage FindPage(ActionContext actionContext, string pageName)
{
var getPageResult = _viewEngine.GetPage(executingFilePath: null, pagePath: pageName);
if (getPageResult.Page != null)
{
return getPageResult.Page;
}
var findPageResult = _viewEngine.FindPage(actionContext, pageName);
if (findPageResult.Page != null)
{
return findPageResult.Page;
}
var searchedLocations = getPageResult.SearchedLocations.Concat(findPageResult.SearchedLocations);
var errorMessage = string.Join(
Environment.NewLine,
new[] { $"Unable to find page '{pageName}'. The following locations were searched:" }.Concat(searchedLocations));
throw new InvalidOperationException(errorMessage);
}
private ActionContext GetActionContext()
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
}
}
}
答案 0 :(得分:10)
这就是我做到的。
始终在Startup.cs中注册服务
services.AddScoped<IViewRenderService, ViewRenderService>();
服务定义如下:
public interface IViewRenderService
{
Task<string> RenderToStringAsync<T>(string viewName, T model) where T : PageModel;
}
public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
private readonly IHttpContextAccessor _httpContext;
private readonly IActionContextAccessor _actionContext;
private readonly IRazorPageActivator _activator;
public ViewRenderService(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider,
IHttpContextAccessor httpContext,
IRazorPageActivator activator,
IActionContextAccessor actionContext)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
_httpContext = httpContext;
_actionContext = actionContext;
_activator = activator;
}
public async Task<string> RenderToStringAsync<T>(string pageName, T model) where T : PageModel
{
var actionContext =
new ActionContext(
_httpContext.HttpContext,
_httpContext.HttpContext.GetRouteData(),
_actionContext.ActionContext.ActionDescriptor
);
using (var sw = new StringWriter())
{
var result = _razorViewEngine.FindPage(actionContext, pageName);
if (result.Page == null)
{
throw new ArgumentNullException($"The page {pageName} cannot be found.");
}
var view = new RazorView(_razorViewEngine,
_activator,
new List<IRazorPage>(),
result.Page,
HtmlEncoder.Default,
new DiagnosticListener("ViewRenderService"));
var viewContext = new ViewContext(
actionContext,
view,
new ViewDataDictionary<T>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
},
new TempDataDictionary(
_httpContext.HttpContext,
_tempDataProvider
),
sw,
new HtmlHelperOptions()
);
var page = ((Page)result.Page);
page.PageContext = new Microsoft.AspNetCore.Mvc.RazorPages.PageContext
{
ViewData = viewContext.ViewData
};
page.ViewContext = viewContext;
_activator.Activate(page, viewContext);
await page.ExecuteAsync();
return sw.ToString();
}
}
}
我称之为
emailView.Body = await this._viewRenderService.RenderToStringAsync("Email/ConfirmAccount", new Email.ConfirmAccountModel
{
EmailView = emailView,
});
“Email / ConfirmAccount”是我的Razor页面的路径(在页面下)。 “ConfirmAccountModel”是该页面的页面模型。
ViewData为null,因为在设置PageContext时设置了Page的ViewData,因此如果未设置,则ViewData为null。
我还发现我必须打电话
_activator.Activate(page, viewContext);
让一切顺利。这尚未经过全面测试,因此可能无法适用于所有情况,但应该可以帮助您入门。
答案 1 :(得分:2)
我遇到了同样的问题。
我查看了RazorViewEngine源代码并找到了该页面 使用&#34;页面&#34;进行搜索路线数据:
var routeData = new RouteData();
routeData.Values.Add("page", "/Folder/MyPage");
它以完整的路径为我工作&#34; / Folder / MyPage&#34;在routeData和页面名称&#34; MyPage&#34;在GetPage调用中。
答案 2 :(得分:2)
如果像我一样,您没有从GetRouteData()
得到_httpContext.HttpContext
并且_actionContext
为空,则可以创建扩展名:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Utils
{
public static class PageExtensions
{
public static async Task<string> RenderViewAsync(this PageModel pageModel, string pageName)
{
var actionContext = new ActionContext(
pageModel.HttpContext,
pageModel.RouteData,
pageModel.PageContext.ActionDescriptor
);
using (var sw = new StringWriter())
{
IRazorViewEngine _razorViewEngine = pageModel.HttpContext.RequestServices.GetService(typeof(IRazorViewEngine)) as IRazorViewEngine;
IRazorPageActivator _activator = pageModel.HttpContext.RequestServices.GetService(typeof(IRazorPageActivator)) as IRazorPageActivator;
var result = _razorViewEngine.FindPage(actionContext, pageName);
if (result.Page == null)
{
throw new ArgumentNullException($"The page {pageName} cannot be found.");
}
var page = result.Page;
var view = new RazorView(_razorViewEngine,
_activator,
new List<IRazorPage>(),
page,
HtmlEncoder.Default,
new DiagnosticListener("ViewRenderService"));
var viewContext = new ViewContext(
actionContext,
view,
pageModel.ViewData,
pageModel.TempData,
sw,
new HtmlHelperOptions()
);
var pageNormal = ((Page)result.Page);
pageNormal.PageContext = pageModel.PageContext;
pageNormal.ViewContext = viewContext;
_activator.Activate(pageNormal, viewContext);
await page.ExecuteAsync();
return sw.ToString();
}
}
}
}
注意:此代码仅呈现正在调用的页面并忽略布局。
您只需要像这样从PageModel
调用它:
var s = this.RenderViewAsync("sendEmail").Result;
"sendEmail"
是您的PageModel view
的名称,路径为/Pages/sendEmail.cshtml
答案 3 :(得分:1)
这是我走的路线。非常简单,可以治疗...
using System;
using System.IO;
using System.Net;
namespace gMIS.Rendering
{
public static class RazorPage
{
public static string RenderToString(string url)
{
try
{
//Grab page
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
return html;
}
catch (Exception err)
{
return {Handle as you see fit};
}
}
}
}
这样称呼。...
var msg = RazorPage.RenderToString(url);
示例:
var pathToRazorPageFolder = request.PathToRazorPageFolder();
var msg = RazorPage.RenderToString($"{pathToRazorPageFolder}/Task_Summary?userGuid={userGuid}&taskId={task.Task_ID}&includelink=true&linkuserGuid={linkUserGuid}");
上面的示例使用了我创建的扩展名,以帮助获取应用程序的基本路径。
namespace Microsoft.AspNetCore.Http
{
public static class RequestExtension
{
public static string PathToRazorPageFolder(this HttpRequest request)
{
if (request != null) {
var requestPath = request.Path.ToString();
var returnPathToFolder = request.Scheme + "://" + request.Host + requestPath.Substring(0, requestPath.LastIndexOf("/")); ;
return returnPathToFolder;
} else
{
return "HttpRequest was null";
}
}
}
}
我知道这不使用依赖注入,但是很简单。而且就可以了。不管如何托管,它都可以与任何页面一起使用。该页面位于您的应用程序之内还是之外。