ASP.NET MVC:Razor - 如何在不向浏览器发送大量空白空间的情况下保持良好的缩进代码 [英] ASP.NET MVC: Razor - How to keep nicely indented code without sending a load of white space to the browser
问题描述
我正在使用razor渲染javascript对象,如下面的代码片段所示
I am using razor to render out javascript objects, as shown in the following code snippet
@{
bool isFirst = true;
foreach (var qa in Model.Form.FormItems)
{
if (isFirst)
{
isFirst = false;
}
else
{
@:,
}
@:new QuestionAndAnswer(
@:@(qa.QuestionAnswerId),
@:@(qa.OrderNumber),
@:@(qa.ParentOrderNumber),
@:@(qa.IsHeader.ToJsonValue()),
@:@(qa.IsMandatory.ToJsonValue()),
@:@(qa.IsAlertable.ToJsonValue()),
@:@(qa.IsAlarmable.ToJsonValue()),
@:@(qa.IsKeyItem.ToJsonValue()),
@:@(qa.IsHiddenQuestion.ToJsonValue()),
@:new Question(
@:@(qa.Question.QuestionId),
@:@Html.Raw(qa.Question.IdCode.ToJsonValue()),
@:new OverridableFormItemText(
@:@(qa.Question.ItemText.DefaultFormItemTextId),
@:@Html.Raw(qa.Question.ItemText.DefaultText.ToJsonValue()),
@:@Html.Raw(qa.Question.ItemText.DefaultHelpText.ToJsonValue()),
..etc...
这使我的cshtml页面易于阅读和布局合理。
This makes my cshtml pages easy to read and well laid out.
不幸的是,所有缩进都会呈现给浏览器,使页面大约需要4倍。 html的示例片段:
Unfortunately, all the indents are rendered to the browser make the page around 4x bigger than it need to be. Example snippet of the html:
new QuestionAndAnswer(
34500,
2,
1,
false,
false,
false,
false,
false,
false,
new Question(
33955,
"123",
new OverridableFormItemText(
23879,
"Locality",
"",
null,
"",
""
)
),
new Answer(
22196,
"321",
4,
"MultipleChoiceSingleSelect",
我是否有办法保留格式良好的服务器端代码但发送未格式化的版本(没有缩进到浏览器节省带宽?
Is there are way for me to retain the nicely formatted server side code but send a unformatted version (ie. no indents) to the browser that saves on bandwidth?
推荐答案
OP在这里!
感谢大家的回答。一切都很有用,但最后我发现这个链接提供了mos优雅的解决方案:
Thanks to everyone for their answers. All useful, but in the end I found this link offered the most elegant solution:
http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20
感谢蒂姆罗杰斯的链接。
Thanks to Tim Rodgers for the link.
这篇关于ASP.NET MVC:Razor - 如何在不向浏览器发送大量空白空间的情况下保持良好的缩进代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!