如何在ASP中为字符串呈现Razor视图。净MVC 3 ?

时间:2021-09-02 11:50:26

I've been searching the site a lot, but all I could find were examples on how to render partial controls .ascx, or depended on a controller context.

我一直在搜索这个站点,但是我所能找到的只是一些关于如何呈现部分控件的例子,ascx,或者依赖于一个控制器上下文。

I want a method that enables me to provide just the relative path to the view, and a model, and render that view with that model into a string:

我想要一种方法,使我能够提供到视图的相对路径和一个模型,并将该模型的视图呈现为一个字符串:

string result = Utility.RenderViewToString("~/Views/My/Profile.cshtml", model);

All the examples I could find were either for older versions of MVC, or simply didn't do what I need to do here.

我能找到的所有例子都是针对旧版本的MVC,或者只是没有做我在这里需要做的事情。

4 个解决方案

#1


27  

You can achieve that with the razorengine.

你可以用razorengine实现这一点。

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

And it does not rely on the controller context - but because of that you are not able to use the Html helpers (which rely on the http context). But it is perfect to use razor as a template engine for strings.

而且它不依赖于控制器上下文—但是由于这个原因,您不能使用Html helper(它依赖于http上下文)。但是使用razor作为字符串的模板引擎是再好不过了。

#2


12  

You can check this link.

你可以查看这个链接。

extracted content From Render Razor view to String. .

从渲染剃刀视图提取内容到字符串。

public static string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        return sw.GetStringBuilder().ToString();
    }
}

#3


10  

I had created solution for me. It's Extension that render view into string.

我为自己创造了解决方案。将视图渲染成字符串的扩展。

public static class RenderPartialToStringExtensions
{
    /// <summary>
    /// render PartialView and return string
    /// </summary>
    /// <param name="context"></param>
    /// <param name="partialViewName"></param>
    /// <param name="model"></param>
    /// <returns></returns>
    public static string RenderPartialToString(this ControllerContext context, string partialViewName, object model)
    {
        return RenderPartialToStringMethod(context, partialViewName, model);
    }

    /// <summary>
    /// render PartialView and return string
    /// </summary>
    /// <param name="context"></param>
    /// <param name="partialViewName"></param>
    /// <param name="viewData"></param>
    /// <param name="tempData"></param>
    /// <returns></returns>
    public static string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
    {
        return RenderPartialToStringMethod(context, partialViewName, viewData, tempData);
    }

    public static string RenderPartialToStringMethod(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
    {
        ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);

        if (result.View != null)
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter output = new HtmlTextWriter(sw))
                {
                    ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);
                    result.View.Render(viewContext, output);
                }
            }

            return sb.ToString();
        }
        return String.Empty;
    }

    public static string RenderPartialToStringMethod(ControllerContext context, string partialViewName, object model)
    {
        ViewDataDictionary viewData = new ViewDataDictionary(model);
        TempDataDictionary tempData = new TempDataDictionary();
        return RenderPartialToStringMethod(context, partialViewName, viewData, tempData);
    }
}

And than we can render view in Action

在实际操作中

[HttpPost]
public ActionResult GetTreeUnit(string id)
{
    int _id = id.ExtractID();
    string render = ControllerContext.RenderPartialToString("SomeView");
    return Json(new { data = render });
}

#4


3  

Though Marc's is answer is correct and works for previous versions. But that is now obselete and needs to be replaced.

尽管马克的回答是正确的,并且适用于以前的版本。但这一点现在很模糊,需要被取代。

Here is the new code that worked for me and you can find more useful information on Github : Razor Engine

这是为我工作的新代码,你可以在Github上找到更多有用的信息:Razor引擎。

string template = "Hello @Model.Name! Welcome to Razor!";
string Result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });

#1


27  

You can achieve that with the razorengine.

你可以用razorengine实现这一点。

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

And it does not rely on the controller context - but because of that you are not able to use the Html helpers (which rely on the http context). But it is perfect to use razor as a template engine for strings.

而且它不依赖于控制器上下文—但是由于这个原因,您不能使用Html helper(它依赖于http上下文)。但是使用razor作为字符串的模板引擎是再好不过了。

#2


12  

You can check this link.

你可以查看这个链接。

extracted content From Render Razor view to String. .

从渲染剃刀视图提取内容到字符串。

public static string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        return sw.GetStringBuilder().ToString();
    }
}

#3


10  

I had created solution for me. It's Extension that render view into string.

我为自己创造了解决方案。将视图渲染成字符串的扩展。

public static class RenderPartialToStringExtensions
{
    /// <summary>
    /// render PartialView and return string
    /// </summary>
    /// <param name="context"></param>
    /// <param name="partialViewName"></param>
    /// <param name="model"></param>
    /// <returns></returns>
    public static string RenderPartialToString(this ControllerContext context, string partialViewName, object model)
    {
        return RenderPartialToStringMethod(context, partialViewName, model);
    }

    /// <summary>
    /// render PartialView and return string
    /// </summary>
    /// <param name="context"></param>
    /// <param name="partialViewName"></param>
    /// <param name="viewData"></param>
    /// <param name="tempData"></param>
    /// <returns></returns>
    public static string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
    {
        return RenderPartialToStringMethod(context, partialViewName, viewData, tempData);
    }

    public static string RenderPartialToStringMethod(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
    {
        ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);

        if (result.View != null)
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter output = new HtmlTextWriter(sw))
                {
                    ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);
                    result.View.Render(viewContext, output);
                }
            }

            return sb.ToString();
        }
        return String.Empty;
    }

    public static string RenderPartialToStringMethod(ControllerContext context, string partialViewName, object model)
    {
        ViewDataDictionary viewData = new ViewDataDictionary(model);
        TempDataDictionary tempData = new TempDataDictionary();
        return RenderPartialToStringMethod(context, partialViewName, viewData, tempData);
    }
}

And than we can render view in Action

在实际操作中

[HttpPost]
public ActionResult GetTreeUnit(string id)
{
    int _id = id.ExtractID();
    string render = ControllerContext.RenderPartialToString("SomeView");
    return Json(new { data = render });
}

#4


3  

Though Marc's is answer is correct and works for previous versions. But that is now obselete and needs to be replaced.

尽管马克的回答是正确的,并且适用于以前的版本。但这一点现在很模糊,需要被取代。

Here is the new code that worked for me and you can find more useful information on Github : Razor Engine

这是为我工作的新代码,你可以在Github上找到更多有用的信息:Razor引擎。

string template = "Hello @Model.Name! Welcome to Razor!";
string Result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });