Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

How to minify in .net core mvc view?

On the website I made with .net core mvc. When we open the site and click on the page source view, how can we do the long codes as shown in the second picture, in the form of minify?

enter image description here

enter image description here

question from:https://stackoverflow.com/questions/65859075/how-to-minify-in-net-core-mvc-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Adding Web Markup Min to ASP.NET Core application

WebMarkupMin is a very mature minifier, not just for HTML but also XML and XHTML, as well as script and style tags embedded in your HTML. They provide multiple NuGet packages for hooking up your ASP.NET applications, both for ASP.NET 4.x using MVC, HttpModules, WebForms and ASP.NET Core.

Step 1. Install package WebMarkupMin.AspNetCoreX

My project is ASP.NET Core 5, so I choose to install WebMarkupMin.AspNetCore5.

enter image description here

Step 2. Register in your application's Startup.Configure method

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        app.UseStaticFiles();

        app.UseWebMarkupMin();

        app.UseRouting();
        ...
    }

Step 3. Register services to the IoC container

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddWebMarkupMin(
            options =>
            {
                options.AllowMinificationInDevelopmentEnvironment = true;
                options.AllowCompressionInDevelopmentEnvironment = true;
            })
            .AddHtmlMinification(
                options =>
                {
                    options.MinificationSettings.RemoveRedundantAttributes = true;
                    options.MinificationSettings.RemoveHttpProtocolFromAttributes = true;
                    options.MinificationSettings.RemoveHttpsProtocolFromAttributes = true;
                })
            .AddHttpCompression();
    }

Test result

Before:

enter image description here

After:

enter image description here



Configure bundling and minification

The MVC and Razor Pages project templates provide a solution for bundling and minification consisting of a JSON configuration file. A third-party tool is a great fit when your development workflow requires processing beyond bundling and minification—such as linting and image optimization. By using design-time bundling and minification, the minified files are created prior to the app's deployment. Bundling and minifying before deployment provides the advantage of reduced server load. You can check details from official document here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...