Reputation: 5089
I've asked this question as a comment on another post but couldn't get it to work. I'm using RenderRazorViewToString() method, but somehow the JSON that get returned got all the Visual studio formatting with it (tabs, white spaces, line-breaks, etc...), here's what I've done:
public virtual ActionResult RenderToString()
{
var html = RenderRazorViewToString(MVC.Markets.Views._RenderToString);
return Json(new { html = html }, JsonRequestBehavior.AllowGet);
}
Here's the result that got returned:
{"html": "<div class=\"frame company-quote contain\" data-module-name=\"FirstGlanceModule\">\r\n\t<div data-module-name=\"CompanyHeaderModule\">\r\n\t\t<h1>\r\n\t\t\tMicrosoft Corp <em>(MSFT:NASDAQ)</em></h1>\r\n\t</div>\r\n\t<div class=\"first-glance\" data-module-name=\"FirstGlanceQuoteModule\">\r\n\t\t<table>\r\n\t\t\t<thead>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<th>\r\n\t\t\t\t\t\tPrice\r\n\t\t\t\t\t</th>\r\n\t\t\t\t\t<th>\r\n\t\t\t\t\t\tChange\r\n\t\t\t\t\t</th>\r\n\t\t\t\t\t<th>\r\n\t\t\t\t\t\tVolume\r\n\t\t\t\t\t</th>\r\n\t\t\t\t\t<th>\r\n\t\t\t\t\t\tMarket Cap\r\n\t\t\t\t\t</th>\r\n\t\t\t\t</tr>\r\n\t\t\t</thead>\r\n\t\t\t<tbody>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td>\r\n\t\t\t\t\t\t$30.13\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t<td>\r\n\t\t\t\t\t\t<span class=\"neg\">-0.13</span>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t<td>\r\n\t\t\t\t\t\t32.75 M\r\n\t\t\t\t\t</td>\r\n\t\t\t\t\t<td>\r\n\t\t\t\t\t\t$252.77 B\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\r\n\t\t\t</tbody>\r\n\t\t</table>\r\n\t</div>\r\n\t<div class=\"controls\" data-module-name=\"FirstGlanceControlsModule\">\r\n\t\t<em>Data as of February 15, 2012 01:59:59 PM ET</em>\r\n\t\t<div>\r\n\t\t\t<button class=\"type-1\">\r\n\t\t\t\t<span>Add to Watchlist</span></button>\r\n\t\t\t<button class=\"type-2\">\r\n\t\t\t\t<span>Trade</span></button>\r\n\t\t</div>\r\n\t</div>\r\n</div>\r\n"}
I've tried to add an extension method outlined here to reformat this into xml with Formatting.None and got a slightly better result, but still have extra tags as well as the xml version tag which I probably don't want:
{"html": "<?xml version=\"1.0\" encoding=\"utf-16\"?><div class=\"frame company-quote contain\" data-module-name=\"FirstGlanceModule\"><div data-module-name=\"CompanyHeaderModule\"><h1>\r\n\t\t\tMicrosoft Corp <em>(MSFT:NASDAQ)</em></h1></div><div class=\"first-glance\" data-module-name=\"FirstGlanceQuoteModule\"><table><thead><tr><th>\r\n\t\t\t\t\t\tPrice\r\n\t\t\t\t\t</th><th>\r\n\t\t\t\t\t\tChange\r\n\t\t\t\t\t</th><th>\r\n\t\t\t\t\t\tVolume\r\n\t\t\t\t\t</th><th>\r\n\t\t\t\t\t\tMarket Cap\r\n\t\t\t\t\t</th></tr></thead><tbody><tr><td>\r\n\t\t\t\t\t\t$30.13\r\n\t\t\t\t\t</td><td><span class=\"neg\">-0.13</span></td><td>\r\n\t\t\t\t\t\t32.75 M\r\n\t\t\t\t\t</td><td>\r\n\t\t\t\t\t\t$252.77 B\r\n\t\t\t\t\t</td></tr></tbody></table></div><div class=\"controls\" data-module-name=\"FirstGlanceControlsModule\"><em>Data as of February 15, 2012 01:59:59 PM ET</em><div><button class=\"type-1\"><span>Add to Watchlist</span></button><button class=\"type-2\"><span>Trade</span></button></div></div></div>"}
Any idea on how to make this work? Thanks.
Upvotes: 1
Views: 2093
Reputation: 4412
Based on this SO question, it looks like there are issues with string.Replace and large strings, since they're immutable, but StringBuilder should work well even on large strings.
public virtual ActionResult RenderToString()
{
string html = RenderRazorViewToString(MVC.Markets.Views._RenderToString);
html = new StringBuilder(html)
.Replace("\n","")
.Replace("\r","")
.Replace("\t","")
.ToString();
return Json(new { html = html }, JsonRequestBehavior.AllowGet);
}
Add any other whitespace characters as needed.
Upvotes: 3
Reputation: 2775
As the view should be returning valid XML, you could try and use something like this:
public virtual ActionResult RenderToString()
{
var html = RenderRazorViewToString(MVC.Markets.Views._RenderToString);
string clean = XElement.Parse(html).ToString(SaveOptions.DisableFormatting);
return Json(new { html = clean }, JsonRequestBehavior.AllowGet);
}
Or there is another neat option here on SO
Upvotes: 1