DEV Community

hdemiray
hdemiray

Posted on

C# MVC Bundle Config İle Minify İşlemleri

Web sitelerinde açılışta her bir javascript ve css dosyaları için requestler gider ve bunların sayısı sitenin açılış hızını düşürebilir.

Bundle minify işlemi ile hem tüm scriptleri tek bir dosya ile gönderecek hemde dosyanın minify edilmesini yani yorum satırları silinmiş gereksiz satırlardan arınmış bir şekilde birleştirecek.

Bunun için ilk önce Projeye Nuget yardımı ile
Microsoft.Asp.Net.Optimization yüklenir
daha sonra yeni bir class açarız ve içerisine static bir metot yazarız ve bu dışarıdan BundleCollection tipinde veri alır

 public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
               "~/Scripts/jquery-3.0.0.js",
               "~/Scripts/jquery.unobtrusive-ajax.min.js",
               "~/Scripts/bootstrap.js",
               "~/Scripts/DataTables/jquery.dataTables.js",
               "~/Scripts/DataTables/dataTables.bootstrap.js",
               "~/Scripts/custom.js",
               "~/Scripts/bootbox.js"
                ));

            bundles.Add(new StyleBundle("~/bundles/styles").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/StyleSheet1.css",
                  "~/Content/DataTables/css/dataTables.bootstrap.css"
                ));
        }
Enter fullscreen mode Exit fullscreen mode

ve buraya dosya yolları eklenir daha sonra layout 'tan bu alanlar silinir
yerlerine ise

@Scripts.Render("~/bundles/scripts")

@styles.Render("~/bundles/styles")

Eklenir daha sonra bu işlemi çağırmak için Global.asax dosyasına

BundleConfig.RegisterBundles(BundleTable.Bundles);

burada kendi yazdığımız sınıfı ve metodu çağırdık ve bundlecollection tipinde bir veri gönderdik.

Son olarak ise View'in altında bulunan webconfig dosyasına namespace ekleyeceğiz

<add namespace="System.Web.Optimization"/>

bu işlem proje yayınlanmadan önce yapılmalı yani bu şekilde proje geliştirmek sağlıklı değildir

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay