在核心 3.0 中将视图渲染为字符串:找不到与 ActionContext 关联的 IRouter

Den*_* W. 10 c# asp.net-core asp.net-core-3.0

我想要的是:

在我的应用程序中,我想为我的电子邮件使用模板。不幸的是,我之前在另一个项目中使用的代码不再起作用。

抛出的错误:

Could not find an IRouter associated with the ActionContext. 

If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection 
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题,因为我找不到任何方法来注入IUrlHelper. 我不确定为什么甚至需要这样做,因为无论如何它都不在视图中。

字符串渲染方法:

Could not find an IRouter associated with the ActionContext. 

If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection 
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
Run Code Online (Sandbox Code Playgroud)

Den*_* W. 22

找到了解决办法:

我认为由于创建 ActionContext 并为其提供一个空的 RouteData 对象会给我一个 IRouter 异常,所以下一个最好的解决方案是看看我是否可以从实际请求中使用 HttpContext。

所以通过依赖注入,我添加了 _httpContextAccessor 并使用了可用的 HttpContext 对象。

为了完整起见,我将分享最终的实现:

要将视图呈现为 HTML:

RenderToString(string, object);

var htmlBody = await Renderer.RenderToString($"/Views/Shared/confirm-email.cshtml", model);
Run Code Online (Sandbox Code Playgroud)

服务:

public class ViewRenderService : IViewRenderService
{
    private readonly IRazorViewEngine _razorViewEngine;
    private readonly ITempDataProvider _tempDataProvider;
    private readonly IHttpContextAccessor _contextAccessor;

    public ViewRenderService(IRazorViewEngine razorViewEngine,
                             ITempDataProvider tempDataProvider,
                             IHttpContextAccessor contextAccessor)
    {
        _razorViewEngine = razorViewEngine;
        _tempDataProvider = tempDataProvider;
        _contextAccessor = contextAccessor;                                                                                            
    }

    public async Task<string> RenderToString(string viewName, object model)
    {
        var actionContext = new ActionContext(_contextAccessor.HttpContext, _contextAccessor.HttpContext.GetRouteData(), new ActionDescriptor());

        await using var sw = new StringWriter();
        var viewResult = FindView(actionContext, viewName);

        if (viewResult == null)
        {
            throw new ArgumentNullException($"{viewName} does not match any available view");
        }

        var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
        {
                Model = model
        };

        var viewContext = new ViewContext(
            actionContext,
            viewResult,
            viewDictionary,
            new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
            sw,
            new HtmlHelperOptions()
        );

        await viewResult.RenderAsync(viewContext);
        return sw.ToString();
    }

    private IView FindView(ActionContext actionContext, string viewName)
    {
        var getViewResult = _razorViewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
        if (getViewResult.Success)
        {
            return getViewResult.View;
        }

        var findViewResult = _razorViewEngine.FindView(actionContext, viewName, isMainPage: true);
        if (findViewResult.Success)
        {
            return findViewResult.View;
        }

        var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
        var errorMessage = string.Join(
            Environment.NewLine,
            new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));

        throw new InvalidOperationException(errorMessage);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它完美地工作。