Handling common JavaScript files in Visual Studio 2010

前端 未结 9 804
说谎
说谎 2020-12-02 15:31

We\'re beginning work on a couple of fully JavaScript-dependent web apps (our previous apps have been ASP.NET MVC, with JavaScript \'goodness\' sprinkled over-the-top).

9条回答
  •  Happy的楠姐
    2020-12-02 16:22

    I can also see this question is ancient, but thought I would add my two cents...

    I have a javascript file in a separate project. I added a linked reference and this works well for publishing, but doesn't work in IIS Express or Casinni. I tried adding custom routing to catch the missing file and manually remap it, but it is bit of a hack and for some reason stopped working when I upgraded to MVC 5.1, so rather than fix the hack, I found a better way:

    System.Web.Optimization has javascript bundles.

    In your shared project, set the Copy To Output Directory to 'Copy Always' and Build Action to 'Content' on your js file. This means your js files end up in the bin folder of your website project. They cannot be served from there (IIS wont serve anything in the bin folder for obvious security reasons), but they can be included in bundles

    using System.Web;
    using System.Web.Optimization;
    
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {   
            bundles.Add(new ScriptBundle("~/bundles/externalLibrary").Include(
                        "~/bin/scripts/externalLibrary.js"
                        ));
        }
    }
    

    You then need to add this to Application_Start in your global.asax file (right next to register routes)

    BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
    

    then use your bundle link this in your razor cshtml:

    
    

    you will need the nuget package for microsoft.aspnet.web.optimization

提交回复
热议问题