Getting Styles.Render() to preserve indentation from Razor template?

倖福魔咒の 提交于 2019-12-10 05:53:11

问题


I'm slightly obsessive about the readability (and hence indentation) of all markup.

When I call @Styles.Render("~/content/css") in an ASP.NET MVC4 project, only the first line maintains indentation from my Razor template.

Here is the output:

    <link href="/Content/css/ie.css" rel="stylesheet"/>
<link href="/Content/css/1140.css" rel="stylesheet"/>
<link href="/Content/css/screen.css" rel="stylesheet"/>
<link href="/Content/css/compatibility.css" rel="stylesheet"/>

I would prefer all generated markup have the same indentation as the @Styles.Render() call.

Is this easily done? If so, how?


回答1:


Ideally the rendered HTML would be minified. Formatted markup is great while developing but makes for a bigger file if that is what you are serving to the user.

The only reason you see four style sheets is that you are running in a debug environment, which has disabled your bundling. As I explained in the post "Scripts.Render using outdated javascript file" if you add BundleTable.EnableOptimizations = true; to the bottom of your RegisterBundles in your BundleConfig, it will force bundling to work (as it would in release mode), and you'll see that this:

    <link href="/Content/css/ie.css" rel="stylesheet"/>
<link href="/Content/css/1140.css" rel="stylesheet"/>
<link href="/Content/css/screen.css" rel="stylesheet"/>
<link href="/Content/css/compatibility.css" rel="stylesheet"/>

now renders as this:

    <link href="/Content/css?v=Sn3f8Vf56Sr9k0EreIZnouVoGt2cfrd41" rel="stylesheet"/>

So yes while developing, it isn't maintaining your indentation, but once you publish it will be what you want.



来源:https://stackoverflow.com/questions/14586562/getting-styles-render-to-preserve-indentation-from-razor-template

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