-2

我需要从特定视图加载生成的字符串,以便将其加载到控制器操作中的 XmlDocument 对象中,以便对其进行其他操作。

我想将该视图用作模板,它会生成 SVG 图像。

一旦我得到结果,我需要作为 XMLDocument 传递给第三方 dll,该 dll 接受 XMLDocument 并将其转换为位图图像

你怎么能这样做?它应该是一个简单的操作,但我没有找到如何去做的线索。

提前致谢

4

1 回答 1

2

The question really is not clear.

If you want to get rendered html as string you can get the string output of the View using the following extension method:

public static string RenderPartialView(this Controller controller, string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

    controller.ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

Use it like this (in the code for the Action):

var model = [whatever is the model that is used by the view]
var renderedView = this.RenderPartialView("Path to the view", model);

Then you can parse this string into document using Html Agility Pack. You can find out how to do it in this answer.

于 2013-07-10T21:25:45.230 回答