简体   繁体   English

ASP.NET MVC:Razor - 如何在不向浏览器发送大量空白空间的情况下保持良好的缩进代码

[英]ASP.NET MVC: Razor - How to keep nicely indented code without sending a load of white space to the browser

I am using razor to render out javascript objects, as shown in the following code snippet 我正在使用razor渲染javascript对象,如下面的代码片段所示

@{
    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...

This makes my cshtml pages easy to read and well laid out. 这使我的cshtml页面易于阅读和布局。

Unfortunately, all the indents are rendered to the browser make the page around 4x bigger than it need to be. 不幸的是,所有缩进都呈现给浏览器使页面大约需要4倍。 Example snippet of the html: 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? 有没有办法让我保留格式良好的服务器端代码,但是向浏览器发送未格式化的版本(即没有缩进)以节省带宽?

You could restructure your Razor code so that you're not dropping in and out on each line. 您可以重新构建Razor代码,这样您就不会在每一行中进出。 Something like this: 像这样的东西:

@{
    bool isFirst = true;
}

@foreach (var qa in Model.Form.FormItems)
{
    @(isFirst ? "" : ",")

    @{
        if (isFirst)
        {
            isFirst = false;
        }
    }

    @* Everything from here on will be rendered as-is in the browser *@
new QuestionAndAnswer(
    @(qa.QuestionAnswerId), 
...
}

To Compress Here is the Code , 要压缩这里是代码,

 protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Implement HTTP compression
       HttpApplication app = (HttpApplication)sender;

        // Retrieve accepted encodings
        string encodings = app.Request.Headers.Get("Accept-Encoding");
        if (encodings != null)
        {
            // Check the browser accepts deflate or gzip (deflate takes preference)
            encodings = encodings.ToLower();
            if (encodings.Contains("deflate"))
            {
                app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "deflate");
            }
            else if (encodings.Contains("gzip"))
            {
                app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "gzip");
            }
        }
    }

在IIS中启用动态内容的压缩,空白将存在,但会很好地压缩。

OP here! OP在这里!

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 http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20

Thanks to Tim Rodgers for the link. 感谢蒂姆罗杰斯的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM