gpt4 book ai didi

asp.net-core-mvc - 在 ASP.NET Core 中呈现包含 URL 到字符串的 Razor View

转载 作者:行者123 更新时间:2023-12-03 18:19:25 25 4
gpt4 key购买 nike

我有一个 Emailer 类,我通过 Dependency Injection 使用它来发送电子邮件,该类获取要在电子邮件中发送的 View 内容。除非 View 包含对底层 URL 帮助程序的调用,否则我的过程效果很好,例如使用这样的 A 标记:

<a asp-controller="Project" asp-action="List">Open</a>

这是我用来将 View 呈现为字符串的代码:
private string renderViewAsString<TModel>(string folder, string viewName, TModel model)
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
var viewEngineResult = _viewEngine.FindView(actionContext, folder + "/" + viewName, false);
var view = viewEngineResult.View;

var viewData = new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
viewData.Model = model;

var tempData = new TempDataDictionary(httpContext, _tempDataProvider);

using (var output = new StringWriter())
{
var viewContext = new ViewContext(actionContext, view, viewData, tempData, output, new HtmlHelperOptions());
var task = view.RenderAsync(viewContext);
task.Wait();

return output.ToString();
}
}

_serviceProvider 是 IServiceProvider 类型,_viewEngine 是 IrazorViewEngine 类型,它们都被注入(inject)到构造函数中。

如果它引用 URL 帮助程序,它会在 task.Wait() 行产生这个异常:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

以此作为调用堆栈:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.get_Router()
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.GetVirtualPathData(String routeName, RouteValueDictionary values)
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.Action(UrlActionContext actionContext)
at Microsoft.AspNetCore.Mvc.UrlHelperExtensions.Action(IUrlHelper helper, String action, String controller, Object values, String protocol, String host, String fragment)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateActionLink(ViewContext viewContext, String linkText, String actionName, String controllerName, String protocol, String hostname, String fragment, Object routeValues, Object htmlAttributes)
at Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Process(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>d__0.MoveNext()

我如何解决这个问题,而不必对 A 元素或电子邮件内容进行硬编码?

最佳答案

我能够让它工作。调用堆栈提到找不到路由器,所以提供它是一个问题:

首先,我在构造函数参数中将其作为 DI 对象添加:

IHttpContextAccessor accessor

这在构造函数中:
_context = accessor.HttpContext;

然后我将功能更改为:
private string renderViewAsString<TModel>(string folder, string viewName, TModel model)
{
var actionContext = new ActionContext(_context, new RouteData(), new ActionDescriptor());
var viewEngineResult = _viewEngine.FindView(actionContext, folder + "/" + viewName, false);
var view = viewEngineResult.View;

var viewData = new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
viewData.Model = model;

var tempData = new TempDataDictionary(_context, _tempDataProvider);

using (var output = new StringWriter())
{
var viewContext = new ViewContext(actionContext, view, viewData, tempData, output, new HtmlHelperOptions());
viewContext.RouteData = _context.GetRouteData(); //set route data here

var task = view.RenderAsync(viewContext);
task.Wait();

return output.ToString();
}
}

关于asp.net-core-mvc - 在 ASP.NET Core 中呈现包含 URL 到字符串的 Razor View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com