简体   繁体   中英

Configure HTML Minification in ASP.NET MVC

I am implementing minification for my project. I have made changes as per documentation .

Here is my updated web.config file:

<webMarkupMin xmlns="http://tempuri.org/WebMarkupMin.Configuration.xsd">
<core>
  <html whitespaceMinificationMode="Medium" removeHtmlComments="true"
     removeHtmlCommentsFromScriptsAndStyles="true"
     removeCdataSectionsFromScriptsAndStyles="true"
     useShortDoctype="true" useMetaCharsetTag="true"
     emptyTagRenderMode="NoSlash" removeOptionalEndTags="true"
     removeTagsWithoutContent="false" collapseBooleanAttributes="true"
     removeEmptyAttributes="true" attributeQuotesRemovalMode="Html5"
     removeRedundantAttributes="true"
     removeJsTypeAttributes="true" removeCssTypeAttributes="true"
     removeHttpProtocolFromAttributes="false"
     removeHttpsProtocolFromAttributes="false"
     removeJsProtocolFromAttributes="true"
     minifyEmbeddedCssCode="true" minifyInlineCssCode="true"
     minifyEmbeddedJsCode="true" minifyInlineJsCode="true"
     processableScriptTypeList="" minifyKnockoutBindingExpressions="false"
     minifyAngularBindingExpressions="false" customAngularDirectiveList="" />
  <css defaultMinifier="KristensenCssMinifier">
    <minifiers>
      <add name="NullCssMinifier" displayName="Null CSS Minifier"
         type="WebMarkupMin.Core.Minifiers.NullCssMinifier, WebMarkupMin.Core" />
      <add name="KristensenCssMinifier"
         displayName="Mads Kristensen's CSS minifier"
         type="WebMarkupMin.Core.Minifiers.KristensenCssMinifier, WebMarkupMin.Core" />

    </minifiers>
  </css>
  <js>
    <minifiers>
      <add name="NullJsMinifier" displayName="Null JS Minifier" type="WebMarkupMin.Core.Minifiers.NullJsMinifier, WebMarkupMin.Core" />
      <add name="CrockfordJsMinifier" displayName="Douglas Crockford's JS Minifier" type="WebMarkupMin.Core.Minifiers.CrockfordJsMinifier, WebMarkupMin.Core" />
    </minifiers>
  </js>
  <logging>
    <loggers>
      <add name="NullLogger" displayName="Null Logger" type="WebMarkupMin.Core.Loggers.NullLogger, WebMarkupMin.Core" />
      <add name="ThrowExceptionLogger" displayName="Throw exception logger" type="WebMarkupMin.Core.Loggers.ThrowExceptionLogger, WebMarkupMin.Core" />
    </loggers>
  </logging>

</core>

and Action method seems like this:

 [MinifyHtmlAttribute]
    public ActionResult Index()
    {
        return View();
    }

This is works fine on my home page. But what if I want to add HTML minifier in all pages of my site? Is there anything else to implement HTML minifier in whole of my project?

Thanks.

Although MinifyHtmlAttribute inherits from ActionFilterAttribute it apparently does not function as a global action filter. So, your only option is to explicitly add [MinifyHtmlAttribute] to each one of the action methods you wish to minify.

That said, the project is open source so it is possible you could invent your own way to configure it that works similar to the [AuthorizeAttribute] and [AllowAnnonymousAttribute] in MVC.

现在,在WebMarkupMin 版本2.XWebMarkupMin.AspNet4.Mvc模块中,您可以在整个应用程序级别应用操作筛选器(请参阅文档的相关部分 )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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