我怎样才能自动格式剃须刀输出与缩进和换行符? [英] How can I auto-format Razor output with indentation and line breaks?

查看:200
本文介绍了我怎样才能自动格式剃须刀输出与缩进和换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Razor页面,包含大量的条件逻辑,循环,部分视图等等。很容易保持输出标记语义上的正确,但是使用正确的缩进和换行符更难以格式化。我怎么能在运行时自动执行此操作?有没有一个模块或Razor扩展?



请不要说右键单击 - >格式选择。要清楚,我想避免在写Razor页面时考虑格式化。我希望我的Razor标记能够以对开发人员有意义的方式进行布局(例如,在服务器端块内部缩进),但是对于单击查看源代码的奇怪人来说,所呈现的HTML将被修饰。 (我不担心增加输出的大小,因为我使用的是gzip / deflate。)

解决方案

使用类似于 TidyNet 的库( http://sourceforge.net / projects / tidynet / ),一个实现 ActionFilter

 <$ c $ b $ {$ b $ if(filterContext.Result is ViewResult)
{
var tidy = new Tidy
{
选项=
{
DocType = DocType,
DropFontTags = DropFontTags,
LogicalEmphasis = LogicalEmphasis,
XmlOut = XmlOut,
Xhtml = Xhtml,
IndentContent = IndentContent,
HideEndTags = HideE ndTags,
MakeClean = MakeClean,
TidyMark = TidyMark,
}
};

filterContext.RequestContext.HttpContext.Response.Filter =
new HtmlTidyFilter(filterContext.RequestContext.HttpContext.Response.Filter,tidy);


$ / code $ / pre

过滤器的算法



$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'计数];
Buffer.BlockCopy(buffer,offset,data,0,count);
string html = Encoding.Default.GetString(buffer);

使用(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));





然后你只需要插入它进入控制器:

pre $
$ {
$ public $ {

$ / code >

Voilá! ;)



我从这个来源得知: http://blog.aquabirdconsulting.com/2009/10/28/asp-net-mvc-clean-html/


I have a few Razor pages with lots of conditional logic, loops, partial views etc. It's easy to keep the output markup semantically correct, but more difficult to format it with the right indentation and line breaks. How could I do this automatically at runtime? Is there a module or Razor extension?

Please don't say Right click -> Format selection. To be clear, I want to avoid having to think about formatting when I write the Razor pages. I want my Razor markup to be laid out in a way that makes sense to developers (indenting inside server-side blocks, for example) - but the rendered HTML to be 'prettified' for the odd person who clicks 'View source'. (I'm not worried about increasing the size of the output because I'm using gzip/deflate.)

解决方案

You can use a library like TidyNet ( http://sourceforge.net/projects/tidynet/ ), an implement an 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);
    }
}

The algorithm of the filter:

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));
        }
    }
} 

And then you just plug it into the Controller:

[TidyHtml]
public class AnyController : Controller

Voilá! ;)

I learned it from this source: http://blog.aquabirdconsulting.com/2009/10/28/asp-net-mvc-clean-html/

这篇关于我怎样才能自动格式剃须刀输出与缩进和换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆