2

我正在使用 Orchard 1.6 并希望使用主题自定义布局等将视图呈现为字符串。

事实上,有果园 cms 将形状渲染为电子邮件模板,但我需要更多细节(无法对该答案添加评论,我没有足够的代表:(,并且无法发送私人消息)

我在控制器中进行测试(将来我想在单独的任务中执行 int )。所以控制器是(模型是具有一些属性的简单类)

var renderer = new ViewRenderer(
    _workContextAccessor,
    _siteThemeService,
    _services);

var shape = ShapeFactory.MyTestShape(Model: model);
var result = renderer.RenderMessage(shape);

渲染器是

public string RenderMessage(dynamic shape)
{
    var httpContext = new ViewRendererHttpContext(new Uri("http://localhost/orchard/"));
    var routeData = new RouteData();
    routeData.DataTokens.Add("IWorkContextAccessor", _workContextAccessor);
    routeData.Values["controller"] = "Dummy";
    var requestContext = new RequestContext(httpContext, routeData);
    var controllerContext = new ControllerContext(requestContext, new DummyController());

    var viewContext = new ViewContext(controllerContext, new ShapeView(shape), new ViewDataDictionary(shape.Model), new TempDataDictionary(), new StringWriter());
    var scope = _workContextAccessor.CreateWorkContextScope(viewContext.HttpContext);
    scope.WorkContext.CurrentTheme = _siteThemeService.GetSiteTheme();
    var page = new ViewRendererWebViewPage(viewContext, new ViewDataDictionary<dynamic>(shape.Model));

    var displayHelperFactory = _services.WorkContext.Resolve<IDisplayHelperFactory>();
    var display = displayHelperFactory.CreateHelper(page.ViewContext, page);
    var data = display(shape).ToHtmlString();
    scope.Dispose();

    return data;
}

ViewRendererHttpContext 只是从 HttpContextBase 派生出来的,非常简单(可以贴出它的代码),DummyController 是从 Controller 派生的空类,ShapeView 是从 IView 派生的 epmty 类。

我的模块中有 View/MyTestShape.cshtml。

问题是var data = display(shape).ToHtmlString();在深入 执行时var result = htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath, displayContext.Value);(类“ShapeTemplateBindingStrategy”,方法“private IHtmlString Render(......)”)我有异常“方法或操作未实现”。

我应该实现什么方法和什么类?

感谢您的任何提示。我的英语很糟糕所以很抱歉。

4

1 回答 1

2

它也不适用于我,我使用的是 Orchard 1.6。曾经有过投票让它发挥作用,但我不确定它是在 Orchard 1.7 中采用的。

这是我在我的项目中执行此操作的方法(我将形状渲染为 ajax DataTables 的字符串),前提是您从控制器进行调用。

在您的服务(或控制器)中,使用 2 个服务:IThemeAwareViewEngine 和 IDisplayHelperFactory:

    private readonly IThemeAwareViewEngine _themeAwareViewEngine;
    private readonly IDisplayHelperFactory _displayHelperFactory;
    public AjaxDataTables(
        IThemeAwareViewEngine themeAwareViewEngine,
        IDisplayHelperFactory displayHelperFactory
        )
    {
        _themeAwareViewEngine = themeAwareViewEngine;
        _displayHelperFactory = displayHelperFactory;
    }

这是获得魔术显示的方法,前提是您有对真实控制器的引用:

    dynamic GetDisplayHelper(Controller controller)
    {
        // We can specify any view name, just to get a View only, the shape template finding will be taken care by DisplayHelperFactory.
        // Here the "Brandking" view is always existed, we can also use something like "Layout" ...
        var viewResult = _themeAwareViewEngine.FindPartialView(controller.ControllerContext, "Branding", false, false);
        var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, new StringWriter());
        return _displayHelperFactory.CreateHelper(viewContext, new ViewDataContainer());
    }
    private class ViewDataContainer : IViewDataContainer
    {
        public ViewDataDictionary ViewData { get; set; }
    }

并将形状渲染为字符串:

var display = GetDisplayHelper(controller);
var str = Convert.ToString(display(shape.Content));

同样,这仅在您从控制器操作中调用时才有效。

HTH,胡伊

于 2013-08-09T04:17:03.003 回答