Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add Bundle Config to Startup.cs for resources to use in Razor Views?

The question. How can I add to Startup.cs (ASP.NET Core project) same configuration as I was doing with App_Start > BundleConfig.cs

When having:

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com
/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));


        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));
    }

and then:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

for example...

like image 599
Sam Avatar asked Mar 25 '17 15:03

Sam


People also ask

Where do I put bundle config in asp net core?

Click on wwwroot folder, select the css file you want minified, then right click and choose bundler & minifier. Then from popup minify file. It will be the same file name with the minified version. Also generate bundleconfig.

What must be done to enable bundling and minification?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".

What are the different ways for bundling and minification in asp net core?

Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.


1 Answers

Bundling and Minification as it existed in MVC5 no longer exists in MVC Core.

Your options are (without delving into the Node ecosystem - which is just as valid but would introduce more concepts):

  • Use the bundling and minification Visual Studio extension.
  • Or use the command line bundling and minification dotnet tool.

Both of these tools operate on the same infrastructure under the covers. They use a bundleConfig.json file to describe the structure of your bundles (what files go in, what file comes out, wether to include sourcemaps, etc.)

Explanations of these two concepts are also available via the documentation.

Instead of having a call to @Scripts.Render() which would produce links to either minified or unminified resources depending on your build environment, you can use taghelpers to swap between links to minified and unminified resources. For example:

<environment names="Development">
    <script src="~/unminified.js"></script>
</environment>
<environment names="Staging,Production">
    <script src="~/bundledandminified.min.js"></script>
</environment>
like image 81
ChadT Avatar answered Nov 07 '22 04:11

ChadT