控制 ASP.Net MVC 中的输出缩进
我的同事非常“热衷”将格式正确且缩进的 html 传送到客户端浏览器。这是为了使页面源代码易于被人阅读。
首先,如果我有一个在站点中多个不同区域使用的部分视图,渲染引擎是否应该自动为我设置缩进格式(例如在 XmlTextWriter 上设置 Formatting 属性)?
其次,我的同事创建了许多 HtmlHelper 扩展方法来写入响应。这些都需要将 CurrentIndent 参数传递给它们。我觉得这味道不对。
谁能帮忙解决这个问题吗?
My colleague is extremely 'hot' on properly formatted and indented html being delivered to the client browser. This is so that the page source is easily readable by a human.
Firstly, if I have a partial view that is used in a number of different areas in my site, should the rendering engine be automatically formatting the indentations for me (ala setting the Formatting property on an XmlTextWriter)?
Secondly, my colleague has created a number of HtmlHelper extension methods for writing to the response. These all require a CurrentIndent parameter to be passed to them. This smells wrong to me.
Can anyone help with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这听起来很难维护。如果有人从 HTML 中删除了外部元素,是否有人会费心更新代码中的 CurrentIndent 值?如今,大多数开发人员通常都会通过 Firebug 查看 HTML,Firebug 会自动设置缩进格式的标记。
如果您确实想通过格式过滤器对 HTML 进行后处理,请尝试 HTML Tidy 的 .NET 端口。
This sounds difficult to maintain. If someone removed an outer element from the HTML, would anyone bother to update the CurrentIndent values in the code? These days most developers usually view their HTML through Firebug anyway, which formats the markup automatically with indentation.
If you really want to post-process HTML through a formatting filter then try a .NET port of HTML Tidy.
浏览器绝对不关心 HTML 缩进有多漂亮。更重要的是,深度嵌套(因此大量缩进)的 HTML 会为页面增加轻微的开销(就下载字节数而言)。当然,您始终可以压缩响应,并且良好缩进的 HTML 更容易支持。
Browsers absolutely don't care how beautiful the HTML indentation is. What's even more, deeply nested (and thus heavily indented) HTML adds a slight overhead to the page (in terms of bytes to download). Granted, you can always compress response and well-indented HTML is nicer to support.
即使出于某种疯狂的原因,它必须“正确”缩进,也不应该按照您同事建议的方式进行。
附加到
HttpApplication
对象的ReleaseRequestState
事件的 HttpModule 应该可以解决问题。当然,您需要想出一个过滤器来处理这种缩进。Even if for some crazy reason it HAS TO be indented "properly", it shouldn't be done the way your colleague suggests.
An HttpModule attached to
ReleaseRequestState
event of theHttpApplication
object should do the trick. And of course, you're going to need to come up with a filter that handles this indenting.与其浪费时间实施会影响所有 HTTP 请求(从而增加 CPU 和带宽开销)的正确缩进解决方案,不如建议您的同事使用 HTML 美化器。这样一来,关心它的人就是支付它的费用的人。
此 Firefox 插件是一个 HTML 验证器,还包含美化功能。请参阅此处的文档。
Rather than waste time implementing a proper indenting solution which would affect all HTTP requests (thus adding CPU and bandwidth overhead), just suggest to your colleague that he use an HTML beautifier. That way the one person that cares about it is the one person that pays the cost of it.
This Firefox plugin is an HTML validator that also includes a beautification function. See the documentation here.