简体   繁体   English

我应该在哪里包括从ASP.NET MVC的主视图插入的视图的javascript参考?

[英]Where should I include a javascript reference for a view that inerhits from a Master view in ASP.NET MVC?

I have a bunch of pages inheriting from a master view. 我有一堆从主视图继承的页面。 One one of these pages I want to use a popular jQuery plugin called " Uploadify ". 这些页面之一是我要使用一种流行的jQuery插件,名为“ Uploadify ”。

Where should I place the reference for the Uploadify javascript file? 我应该在哪里放置Uploadify javascript文件的引用? I only need it on one small page so it didn't seem right to place it in Master view. 我只需要在一个小页面上放置它,因此将其放置在“母版”视图中似乎不合适。 Is it 'Ok' to place it in the content tag of my inherited view that will use this plugin? 是否可以将其放置在将使用此插件的继承视图的content标签中?

The best way I know to do this is to add a ContentPlaceHolder in the <head> section of the MasterPage , and add the <script> tags to a Content referencing that section in all pages that need extra javascripts (or stylehseets, or whatever... It certainly adds an extra degree of freedom). 我所知道的最好方法是在MasterPage<head>部分中添加ContentPlaceHolder ,并将<script>标记添加到在需要额外JavaScript(或stylehseets等)的所有页面中引用该部分的Content 。 ..它无疑增加了额外的自由度)。

In you master: 在您的主人中:

<head>
    <!-- Sitewide script references, title tags etc goes here -->

    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>

As it is empty by default, you don't have to change anything in any other pages to make this change to your master. 由于默认情况下为空,因此您无需在其他任何页面上进行任何更改即可对母版进行此更改。

In your page that needs the extra js script: 在您的页面中需要额外的js脚本:

<asp:Content ID="HeadContentFromPage" ContentPlaceHolderId="HeadContent">
    <script type="text/javascript" src="myPageSpecificScript.js"></script>
</asp:Content>

Put a ScriptManager control on your master page and include the commonly used JS files there: ScriptManager控件放在母版页上,并在其中包括常用的JS文件:

<asp:ScriptManager ID="ScriptManager1" runat="server" >
  <Scripts>
    <asp:ScriptReference Path="Common.js" />
  </Scripts>
</asp:ScriptManager>

Put a ScriptManagerProxy control on your content pages (or on any user controls) and include the specific JS files there: ScriptManagerProxy控件放在内容页面(或任何用户控件)上,并在其中包括特定的JS文件:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" >
  <Scripts>
    <asp:ScriptReference Path="Specific.js" />
  </Scripts>
</asp:ScriptManagerProxy>

EDIT : I'm not sure if this also works with ASP.NET MVC . 编辑 :我不确定这是否也适用于ASP.NET MVC

You could add a content block in the portion of your master page. 您可以在母版页的一部分中添加内容块。 That would keep the reference to your js file in the document head and out of the document body. 这样会将对您的js文件的引用保留在文档头中,并保留在文档主体之外。

Also, if you use static document caching, it probably doesn't matter much whether you put the document in your master page or in the view, especially if most people will eventually upload a document. 另外,如果您使用静态文档缓存,则将文档放置在母版页还是视图中可能无关紧要,特别是如果大多数人最终都会上传文档。 Although this approach may make the first page load a little bit slower. 尽管这种方法可能会使首页加载速度变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM