Using scripts in a master page with ASP.NET MVC

前端 未结 11 1291
难免孤独
难免孤独 2020-11-28 19:06

I\'m fairly new to ASP.NET MVC, and I\'m having a little trouble with scripts... in particular, I want to use jQuery in most pages, so it makes sense to put it in the master

相关标签:
11条回答
  • 2020-11-28 19:31

    Why not just point your master page at Google's js file hosting? Then even when it comes to deployment (assuming your site is Net facing) you can abuse possibly pre-cached jquery files?

    0 讨论(0)
  • 2020-11-28 19:31

    I just use a slash(/). For Example:

    <script src="/Script/jquery-1.4.1.js"></script>
    

    when the "jquery-1.4.1.js" file is in the Script directory of root. And it works perfectly.

    0 讨论(0)
  • 2020-11-28 19:34

    I made some of what OJ mentions, I created a GoogleHelper class with this methods

    public static string ReferenceGoogleAPI()
    {
        var appSettings = new AppSettingsReader();
        string apiKey = appSettings.GetValue("GoogleApiKey", typeof(string)).ToString();
        return ReferenceGoogleAPI(apiKey);
    }
    
    public static string ReferenceGoogleAPI(string key)
    {
        return "<script type=\"text/javascript\" src=\"http://www.google.com/jsapi?key=" + key + "\"></script>";
    }
    
    public static string ReferenceGoogleLibrary(string name, string version)
    {
        return "<script type=\"text/javascript\">google.load(\"" + name + "\", \"" + version + "\");</script>";
    }
    

    Now I'm adding extra methods to get some ClientLocation data ;)

    0 讨论(0)
  • 2020-11-28 19:41

    You can try Url.Content extension method with Razor syntax

    <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
    
    0 讨论(0)
  • 2020-11-28 19:54

    I think the simplest way is to use the following, and it works in views to.

    <script type="text/javascript" src="<%=ResolveUrl("~/Scripts/myscript.js") %>">
    
    </script>
    
    0 讨论(0)
提交回复
热议问题