Handling common JavaScript files in Visual Studio 2010

前端 未结 9 765
说谎
说谎 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条回答
  • 2020-12-02 16:15

    The correct solution is embedding javascript/css files in your project. You can do this by using WebResources.axd. This is the official way microsoft embeds js in its controls. (Like validation controls)

    You can find a good tutorial on: http://www.4guysfromrolla.com/articles/080906-1.aspx

    0 讨论(0)
  • 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:

    <script type='text/javascript' src='@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/externalLibrary")'></script>
    

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

    0 讨论(0)
  • 2020-12-02 16:22

    This blog post describes an alternative solution to the answer by @beardtwizzle:

    http://consultingblogs.emc.com/jamesdawson/archive/2008/06/03/using-linked-files-with-web-application-projects.aspx

    The idea is similar:

    1. Add the shared file to to web project as a link
    2. Modify the _CopyWebApplication build step in the project, so that the linked files are copied correct destination path.

    So instead of a post build event the files are copied by a modified build step. For me this solution feels a little bit cleaner (but this may well be a matter of taste). Anyway I just added this to our solution and it works great so far!

    0 讨论(0)
提交回复
热议问题