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

时光怂恿深爱的人放手 提交于 2019-12-21 17:20:04

问题


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

This makes my cshtml pages easy to read and well laid out.

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?


回答1:


You could restructure your Razor code so that you're not dropping in and out on each line. 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), 
...
}



回答2:


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



回答3:


Enable compression of dynamic content in IIS, the whitespace will be there but will compress very well.




回答4:


OP here!

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.



来源:https://stackoverflow.com/questions/7121371/asp-net-mvc-razor-how-to-keep-nicely-indented-code-without-sending-a-load-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!