Meh*_*hdi 2 c# asp.net-core razor-pages
我想将页面呈现为电子邮件模板。
但是当渲染器想要使用我遵循本教程的模型时,我收到错误
:learnrazorpages.com
RazorRenderer.cs:
public class RazorRenderer : IRazorRenderer
{
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public RazorRenderer(
IRazorViewEngine viewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_viewEngine = viewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public async Task<string> ToStringAsync<TModel>(string partialName, TModel model)
{
var actionContext = GetActionContext();
var partial = FindView(actionContext, partialName);
var viewData = new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var tempData = new TempDataDictionary(actionContext.HttpContext, _tempDataProvider);
viewData.Model = model;
await using var stringWriter = new StringWriter();
var viewContext = new ViewContext(
actionContext,
partial,
viewData,
tempData,
stringWriter,
new HtmlHelperOptions()
);
await partial.RenderAsync(viewContext);
return stringWriter.ToString();
}
private IView FindView(ActionContext actionContext, string partialName)
{
var getPartialResult = _viewEngine.GetView(null, partialName, false);
if (getPartialResult.Success)
{
return getPartialResult.View;
}
var findPartialResult = _viewEngine.FindView(actionContext, partialName, false);
if (findPartialResult.Success)
{
return findPartialResult.View;
}
var searchedLocations = getPartialResult.SearchedLocations.Concat(findPartialResult.SearchedLocations);
var errorMessage = string.Join(
Environment.NewLine,
new[]
{
$"Unable to find partial '{partialName}'. 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());
}
}
Run Code Online (Sandbox Code Playgroud)
测试.cs:
public class TestModel : PageModel
{
private readonly IRazorRenderer _razorRenderer;
public RecoveryModel(IRazorRenderer razorRenderer)
{
_razorRenderer = razorRenderer;
}
public void OnGet()
{
}
public async Task<IActionResult> OnPost()
{
var body = await _razorRenderer.ToStringAsync(
"Email",
new Test
{
Email = "Email@Test.com"
});
return Content(body);
}
}
Run Code Online (Sandbox Code Playgroud)
电子邮件.cshtml.cs
public class EmailModel : PageModel
{
public string Email { get; set; }
public void OnGet()
{
}
}
Run Code Online (Sandbox Code Playgroud)
电子邮件.cshtml:
@page
@model EmailModel
@{
Layout = null;
}
@Model.Email // Here I get the exception (Model is null)
Run Code Online (Sandbox Code Playgroud)
我哪里做错了?
我哪里做错了?
您需要创建一个局部视图,它是剃刀视图而不是剃刀页面。
电子邮件.cshtml:
@model Test
@{
Layout = null;
}
@Model.Email
Run Code Online (Sandbox Code Playgroud)
模型:
public class Test
{
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
测试型号:
public async Task<IActionResult> OnPost()
{
var body = await _razorRenderer.ToStringAsync(
"Email",
new Test
{
Email = "Email@Test.com"
});
return Content(body);
}
Run Code Online (Sandbox Code Playgroud)
结果: