user3296338
user3296338

Reputation: 87

How to migrate bundleConfig.cs from project asp.net mvc to asp.net core?

I need to migrate bundleConfig from an old asp.net project mvc to asp.net core

I added in startup.cs:

app.UseBundling (
                // as we rebased the static files to / static (see UseStaticFiles ()),
                // we need to pass this information to the bundling middleware as well,
                // otherwise urls would be rewrited incorrectly
                new BundlingOptions
                {
                    StaticFilesRequestPath = "/static"
                },
                bundles =>
                {
                    bundles.AddJs ("/bundles /jquery")
                        .Include ("/Scripts/jquery-{version}.js");

                    bundles.AddCss ("/bundles/jquery")
                        .Include ("/Scripts/jquery-{version}.js");

                    // we use LESS in this demo
                    bundles.AddCss ("/site.css")
                        .Include ("/less/site.less");

                    // defines a Javascript bundle containing your Vue application and components
                    bundles.AddJs ("/app.js")
                        .Include ("/js/components /*.Js")
                        .Include ("/js/app.js");
                });

Upvotes: 2

Views: 3735

Answers (2)

Chaitanya Gadkari
Chaitanya Gadkari

Reputation: 2807

If you are using .Net core, bundleconfig.cs is gone.

Follow the steps below to add bundling and minification

  1. Install the bundler and minifier extension in visual studio
  2. Add bundleconfig.json to you solution
  3. Based on your bundles modify file, something like below
[  
  {  
    "outputFileName": "wwwroot/bundles/bootstrap",  
    "inputFiles": [  
      "Scripts/bootstrap.js"  
    ]  
  },  
  {  
    "outputFileName": "wwwroot/css/site.min.css",  
    "inputFiles": [  
      "wwwroot/css/site.css",
      "Content/bootstrap.css"  
    ],  
    "minify": {  
      "enabled": true,  
      "renameLocals": true  
    },  
    "sourceMap": false  
  },  
  {  
    "outputFileName": "wwwroot/js/semantic.validation.min.js",  
    "inputFiles": [  
      "wwwroot/js/semantic.validation.js"  
    ],  
    "minify": {  
      "enabled": true,  
      "renameLocals": true  
    }  
  }  
]
  1. You can automate the bundling and minification as part of build process as well with dotnet bundle command ( However this part I have not done yet, so not idea on this)

Upvotes: 1

Danimal
Danimal

Reputation: 333

You would have to create a cs file in App_Start

using System.Web.Optimization;

namespace EnterYourProgramsNameSpaceHere
{
    public class BundleConfig
    {

        public static void RegisterBundles(BundleCollection bundles)
        {  
            //Add your bundles here 
            bundles.Add(new StyleBundle("~/Content/myStyle")
            .Include("~/Content/styles.css"));

            bundles.Add(new ScriptBundle("~/bundles/myjs")
             .Include("~/Scripts/jquery.js"));
        }
    }
}

in startup.cs add this line

 BundleConfig.RegisterBundles(BundleTable.Bundles);

You have to make sure that the spacing in your file paths is removed, in your example the program may have an issue finding your js/css files.

you can look up more info here : http://go.microsoft.com/fwlink/?LinkId=301862

Upvotes: -2

Related Questions