如何将ASP.MVC视图的内容保存为字符串?

时间:2022-06-24 07:37:39

I need to save the HTML output of an ASP.MVC 3 View() as a string that I can send to another web service. When I call "return view()" it obviousy renders the output to the screen. I just want to capture that to a string of HTML. Possible?

我需要将ASP.MVC 3 View()的HTML输出保存为我可以发送到另一个Web服务的字符串。当我调用“return view()”时,它显然会将输出呈现给屏幕。我只是想把它捕获到一串HTML。可能?

2 个解决方案

#1


1  

You can use ViewContext to have the MVC framework render the view to a TextWriter class like StringWriter then retrieve the rendered string from that object.

您可以使用ViewContext让MVC框架将视图呈现给TextWriter类,如StringWriter,然后从该对象检索呈现的字符串。

public string RenderViewToString(ControllerContext context, ViewResultBase viewBase)
{
    using (var sw = new StringWriter())
    {
        var view = viewBase.View;
        var viewContext = new ViewContext(context, view, viewBase.ViewData,
                                          viewBase.TempData, sw);
        view.Render(viewContext, sw);
        return sw.ToString();
    }
}

#2


-1  

Create a controller that renders the html view as you want it to look. Don't include anything in the html of the view that you don't want in the string

创建一个控制器,根据您的需要呈现html视图。不要在视图的html中包含您不希望在字符串中的任何内容

public ActionResult HtmlView()
{
     return View();
}


public static string GetContent(string templatePath,  string queryString = "")
    {
        bool isLocal = false;


        if (HttpContext.Current != null)
            isLocal = HttpContext.Current.Request.IsLocal;

        try
        {
            var objWebClient = new System.Net.WebClient();
            byte[] requestedHtml;

            if (isLocal)
            {

                string path = "http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":" +
                              HttpContext.Current.Request.ServerVariables["SERVER_PORT"] + templatePath +
                              queryString;
                requestedHtml = objWebClient.DownloadData(path);
            }
            else
            {
                requestedHtml = objWebClient.DownloadData("http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + templatePath + queryString);
            }
            var utf8 = new UTF8Encoding();

            return utf8.GetString(requestedHtml).Replace(Environment.NewLine, "");
        }
        catch (Exception e)
        {
            return "";
        }
    }

In your code you can then call

在您的代码中,您可以调用

string newString = GetContent("/Controller/HtmlView");

#1


1  

You can use ViewContext to have the MVC framework render the view to a TextWriter class like StringWriter then retrieve the rendered string from that object.

您可以使用ViewContext让MVC框架将视图呈现给TextWriter类,如StringWriter,然后从该对象检索呈现的字符串。

public string RenderViewToString(ControllerContext context, ViewResultBase viewBase)
{
    using (var sw = new StringWriter())
    {
        var view = viewBase.View;
        var viewContext = new ViewContext(context, view, viewBase.ViewData,
                                          viewBase.TempData, sw);
        view.Render(viewContext, sw);
        return sw.ToString();
    }
}

#2


-1  

Create a controller that renders the html view as you want it to look. Don't include anything in the html of the view that you don't want in the string

创建一个控制器,根据您的需要呈现html视图。不要在视图的html中包含您不希望在字符串中的任何内容

public ActionResult HtmlView()
{
     return View();
}


public static string GetContent(string templatePath,  string queryString = "")
    {
        bool isLocal = false;


        if (HttpContext.Current != null)
            isLocal = HttpContext.Current.Request.IsLocal;

        try
        {
            var objWebClient = new System.Net.WebClient();
            byte[] requestedHtml;

            if (isLocal)
            {

                string path = "http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":" +
                              HttpContext.Current.Request.ServerVariables["SERVER_PORT"] + templatePath +
                              queryString;
                requestedHtml = objWebClient.DownloadData(path);
            }
            else
            {
                requestedHtml = objWebClient.DownloadData("http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + templatePath + queryString);
            }
            var utf8 = new UTF8Encoding();

            return utf8.GetString(requestedHtml).Replace(Environment.NewLine, "");
        }
        catch (Exception e)
        {
            return "";
        }
    }

In your code you can then call

在您的代码中,您可以调用

string newString = GetContent("/Controller/HtmlView");