您可以使用类似TidyNet
(http://sourceforge.net/projects/tidynet/)的库,实现ActionFilter
:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result is ViewResult)
{
var tidy = new Tidy
{
Options =
{
DocType = DocType,
DropFontTags = DropFontTags,
LogicalEmphasis = LogicalEmphasis,
XmlOut = XmlOut,
Xhtml = Xhtml,
IndentContent = IndentContent,
HideEndTags = HideEndTags,
MakeClean = MakeClean,
TidyMark = TidyMark,
}
};
filterContext.RequestContext.HttpContext.Response.Filter =
new HtmlTidyFilter(filterContext.RequestContext.HttpContext.Response.Filter, tidy);
}
}
过滤器的算法:
public override void Write(byte[] buffer, int offset, int count)
{
var data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string html = Encoding.Default.GetString(buffer);
using (var input = new MemoryStream())
{
using (var output = new MemoryStream())
{
byte[] byteArray = Encoding.UTF8.GetBytes(html);
input.Write(byteArray, 0, byteArray.Length);
input.Position = 0;
_tidy.Parse(input, output, new TidyMessageCollection());
string result = Encoding.UTF8.GetString(output.ToArray());
byte[] outdata = Encoding.Default.GetBytes(result);
_stream.Write(outdata, 0, outdata.GetLength(0));
}
}
}
然后你只需将它插入控制器:
[TidyHtml]
public class AnyController : Controller
瞧!;)
我是从这个来源了解到的:http: //blog.aquabirdconsulting.com/2009/10/28/asp-net-mvc-clean-html/