仅当 Javascript 尚未包含在 .NET MVC 中时才包含 Javascript

发布于 2024-12-28 19:50:10 字数 877 浏览 1 评论 0原文

在 .NET MVC 部分视图中,我想做这样的事情:

<% ScriptCollection.RequireScript("path/to/script.js"); %>

这需要 ScriptCollection 对象中的一个方法,如下所示:

public void RequireScript(string src)
{
    if (!_List.Contains(src))
        _List.Add(src);
}

然后,在我的母版页中,我会有一些东西像这样:

<html>
<head></head>
<body>

    <!-- Content would go here -->

    <% foreach (var script in ScriptCollection) { %>
        <script type="text/javascript" src="<%= script %>"></script>
    <% } %>

</body>
</html>

我的问题是这样的: 如何使 ScriptCollection 对象可通过部分视图进行更新并可供母版页使用?

编辑: 我不想在控制器中添加所需的脚本,也不想使用强类型母版页。如果这些方法被认为是最佳实践,那么欢迎提出这样做​​的建议。

编辑#2: 使用扩展属性这会很容易。我可以为 HtmlHelper 类提供 ScriptCollection 属性。我是否错过了类似已经存在的东西?

In a .NET MVC partial view, I'd like to do something like this:

<% ScriptCollection.RequireScript("path/to/script.js"); %>

That would require a method in the ScriptCollection object that looks like this:

public void RequireScript(string src)
{
    if (!_List.Contains(src))
        _List.Add(src);
}

Then, in my Master page, I would have something like this:

<html>
<head></head>
<body>

    <!-- Content would go here -->

    <% foreach (var script in ScriptCollection) { %>
        <script type="text/javascript" src="<%= script %>"></script>
    <% } %>

</body>
</html>

My question is this:
How can I make the ScriptCollection object available to be updated by a partial view and also available to be used by a Master page?

Edit:
I do not want to add the required scripts in a controller, nor do I want to use a strongly typed Master page. Though suggestions on doing so are welcome if those methods are considered a best practice.

Edit #2:
This would be easy with extension properties. I could just give the HtmlHelper class the ScriptCollection property. Am I missing something like this that already exists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2025-01-04 19:50:10

我检查了 Telerik 组件。对于我的需要来说,它们有点过分了。另外,开源许可证 (GPL v2) 不是我正在寻找的类型。

我启动了一个根据 MIT 开源许可证获得许可的项目,并将其托管在 Google Code 上。这是解决我的问题的轻量级解决方案。

code.google.com/p/script-keeper

I checked out the Telerik assemblies. They're a little overkill for what I need. Also, the open-source license (GPL v2) isn't the type I'm looking for.

I started a project licensed under the MIT open-source license and am hosting it on Google Code. It's a lightweight solution to my problem.

code.google.com/p/script-keeper

茶花眉 2025-01-04 19:50:10

您可以查看 Telerik 的 ScriptRegisterBuilder 类实现完全符合您的要求。或者你也可以直接使用它。此外,它还支持:

  1. 将多个 JS 文件合并为一个,从而减少浏览器的请求。
  2. 压缩JS文件。
  3. HTTP 缓存。
  4. 内容交付网络。
  5. JS 资源的发布/调试版本取决于您的构建模式。

现在,即使您不使用 Telerik 提供的任何其他内容,您也可以使用 ScriptRegisterBuilder 类,但建议禁用自动 jquery 和 ScriptRegisterBuilder 类。 jquery验证脚本注册。另请参阅提供的链接。

另请查看 StyleSheetRegistrarBuilder 类。这是 ScriptRegisterBuilder 类的侄子,负责处理 CSS 资源。

我确实注意到您问题中的 MVC2 标签,但我的简单示例使用 razor,只是为了说明这一点。让它也适用于 MVC2/ASPX 应该不难。否则请告诉我。

主视图

@using Telerik.Web.Mvc.UI
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>  
    @Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site.css"))  
</head>
<body>
    @RenderBody()

    @Html.Telerik().ScriptRegistrar().jQuery(false).jQueryValidation(false)    
</body>
</html>

局部视图

 @using Telerik.Web.Mvc.UI
 @{
   ViewBag.Title = "Home Page";
   Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site2.css"));
   Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Add("~/Scripts/Test.js"));
  }

  <h2>@ViewBag.Title</h2>
  <p>
     Bla bla bla
  </p>

You could take a look at Telerik's ScriptRegisterBuilder class implementation which does exactly what you want. Or you can just use it as well. In addition it also has support for:

  1. Combining multiple JS files into one which result in less requests by the browser.
  2. Compressing JS files.
  3. HTTP Caching.
  4. Content Delivery Networks.
  5. release/debug versions of your JS resources depending on your build mode.

Now even when you don't use any of the other stuff Telerik provides you can use the ScriptRegisterBuilder class but then it's advised to disable the automatic jquery & jquery validation script registration. See also the link provided.

Also take a look the StyleSheetRegistrarBuilder class. This is the nephew of the ScriptRegisterBuilder class and handles the CSS assets.

I did noticed the MVC2 tag in your question but my simple example uses razor and is just for making the point. It shouldn't be hard to make it work for MVC2/ASPX as well. Otherwise let me know.

Master

@using Telerik.Web.Mvc.UI
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>  
    @Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site.css"))  
</head>
<body>
    @RenderBody()

    @Html.Telerik().ScriptRegistrar().jQuery(false).jQueryValidation(false)    
</body>
</html>

Partial View

 @using Telerik.Web.Mvc.UI
 @{
   ViewBag.Title = "Home Page";
   Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("~/Content/Site2.css"));
   Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Add("~/Scripts/Test.js"));
  }

  <h2>@ViewBag.Title</h2>
  <p>
     Bla bla bla
  </p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文