如何将脚本包包含在脚本中

时间:2022-08-16 20:35:50

I have a layout with an @RenderSection("scripts") section and I have a bundle that would need to be included in this section for some views. I thought that just doing this in the view would work, but it's not rendering the scripts.

我有一个带有@RenderSection(“scripts”)部分的布局,并且我有一个包,需要包含在这个部分中,用于某些视图。我认为在视图中这样做是可行的,但它不会呈现脚本。

@section scripts {
    @Scripts.Render("~/bundles/myBundle")  
}

In my view, how can I include a bundle to the scripts section?

在我看来,如何将一个包包含到脚本部分?

Layout

布局

@Scripts.Render("~/bundles/jquery", "~/bundles/scripts")
@RenderSection("scripts", required: false)

View

视图

@section scripts {
    @Scripts.Render("~/bundles/movie")  
}

4 个解决方案

#1


13  

Why mix the rendersection with bundling? If you choose to down the route of bundling, you can simply put your scripts in .JS file ,put it in their own bundle if you like and call that bundle on your view. For ex:

为什么要混合渲染和捆绑?如果您选择打包,您可以将脚本放在. js文件中,如果您愿意,可以将其放在自己的包中,并在视图中调用该包。为例:

     bundles.Add(new ScriptBundle("~/bundles/myscripts").Include(
                    "~/Scripts/myscript1.js",
                    "~/Scripts/myscript2.js")); 

then view will have the following:

视图将有以下内容:

    @Scripts.Render("~/bundles/myscripts")   

Also make sure your Web.config has compilation debug set to false like below:

还要确保你的网络。配置的编译调试设置为false,如下所示:

  <compilation debug="false" />            

it makes sure the scripts are bundled and minified.

它确保脚本被打包和缩小。

Update

更新

Based on comments and my recent experience, I can see why would we want to use the two together. Perfect case of learning in the community! :) So, if you decide to come back for refactoring, I would make sure there are no typos to begin with. If it still does not work, please let me know what the problem is and I will update the answer accordingly. Thanks everyone!

基于评论和我最近的经验,我明白为什么我们要把这两者结合在一起。在社区学习的完美案例!所以,如果你决定回来进行重构,我将确保开始时没有打字错误。如果还是不行,请让我知道是什么问题,我会相应地更新答案。谢谢大家!

#2


36  

Try below mentioned solution inside the View.

在视图中尝试下面提到的解决方案。

View

视图

@section Scripts{
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/movie")"></script>
}

#3


2  

I can not replicate the issue you are having

我不能重复你所面临的问题

@section scripts {
    @Scripts.Render("~/bundles/movie")  
}

renders fine with no issue (and respects the debug flag) i would agree with @Mrchref and revisit your paths

如果没有问题(并且尊重调试标志),我将同意@Mrchref并重新访问您的路径。

on the other hand, you could use something like this:

另一方面,你可以用这样的东西:

public static class Helpers
{
    public static HtmlString StaticContent(this System.Web.Mvc.UrlHelper url, string contentPath)
    {
        return new HtmlString(System.Web.Optimization.Scripts.Render(contentPath).ToString());
    }
}

Usage:

用法:

@section Scripts{
    @Url.StaticContent("~/assets/js/jquery")
}

i would advise not to use it as is, You would need to find another solution for System.Web.Optimization.Scripts.Render(contentPath).ToString() since it basically renders the same function you are using (and not working).

我建议您不要使用它,您需要为system . web. optimiz . scripts.render (contentPath). tostring()找到另一个解决方案,因为它基本上呈现了您正在使用的同一个函数(不工作)。

You should play around with System.Web.Optimization.BundleTable.Bundles and see if you can query for both version, check the debug flag, and serve the correct content.

您应该使用System.Web.Optimization.BundleTable。捆绑并查看是否可以查询两个版本,检查调试标志,并提供正确的内容。

#4


1  

I believe a new build would solve your problem. Please follow these steps if not:

我相信新的版本会解决你的问题。如果没有,请遵循以下步骤:

  • Step 1: add this into BundleConfig.cs

    步骤1:将此添加到BundleConfig.cs中

    bundles.Add(new ScriptBundle("~/bundles/common").Include("~/Scripts/common.js"));

    包。添加(新ScriptBundle(~ /包/普通)其中包括(~ /脚本/ common.js "));

  • Step 2: add this line in your view/layout

    步骤2:在视图/布局中添加这一行

    @Scripts.Render("~/bundles/common")

    @Scripts.Render(“~ /包/普通”)

  • Step 3: build your project and run it.

    步骤3:构建项目并运行它。

#1


13  

Why mix the rendersection with bundling? If you choose to down the route of bundling, you can simply put your scripts in .JS file ,put it in their own bundle if you like and call that bundle on your view. For ex:

为什么要混合渲染和捆绑?如果您选择打包,您可以将脚本放在. js文件中,如果您愿意,可以将其放在自己的包中,并在视图中调用该包。为例:

     bundles.Add(new ScriptBundle("~/bundles/myscripts").Include(
                    "~/Scripts/myscript1.js",
                    "~/Scripts/myscript2.js")); 

then view will have the following:

视图将有以下内容:

    @Scripts.Render("~/bundles/myscripts")   

Also make sure your Web.config has compilation debug set to false like below:

还要确保你的网络。配置的编译调试设置为false,如下所示:

  <compilation debug="false" />            

it makes sure the scripts are bundled and minified.

它确保脚本被打包和缩小。

Update

更新

Based on comments and my recent experience, I can see why would we want to use the two together. Perfect case of learning in the community! :) So, if you decide to come back for refactoring, I would make sure there are no typos to begin with. If it still does not work, please let me know what the problem is and I will update the answer accordingly. Thanks everyone!

基于评论和我最近的经验,我明白为什么我们要把这两者结合在一起。在社区学习的完美案例!所以,如果你决定回来进行重构,我将确保开始时没有打字错误。如果还是不行,请让我知道是什么问题,我会相应地更新答案。谢谢大家!

#2


36  

Try below mentioned solution inside the View.

在视图中尝试下面提到的解决方案。

View

视图

@section Scripts{
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/movie")"></script>
}

#3


2  

I can not replicate the issue you are having

我不能重复你所面临的问题

@section scripts {
    @Scripts.Render("~/bundles/movie")  
}

renders fine with no issue (and respects the debug flag) i would agree with @Mrchref and revisit your paths

如果没有问题(并且尊重调试标志),我将同意@Mrchref并重新访问您的路径。

on the other hand, you could use something like this:

另一方面,你可以用这样的东西:

public static class Helpers
{
    public static HtmlString StaticContent(this System.Web.Mvc.UrlHelper url, string contentPath)
    {
        return new HtmlString(System.Web.Optimization.Scripts.Render(contentPath).ToString());
    }
}

Usage:

用法:

@section Scripts{
    @Url.StaticContent("~/assets/js/jquery")
}

i would advise not to use it as is, You would need to find another solution for System.Web.Optimization.Scripts.Render(contentPath).ToString() since it basically renders the same function you are using (and not working).

我建议您不要使用它,您需要为system . web. optimiz . scripts.render (contentPath). tostring()找到另一个解决方案,因为它基本上呈现了您正在使用的同一个函数(不工作)。

You should play around with System.Web.Optimization.BundleTable.Bundles and see if you can query for both version, check the debug flag, and serve the correct content.

您应该使用System.Web.Optimization.BundleTable。捆绑并查看是否可以查询两个版本,检查调试标志,并提供正确的内容。

#4


1  

I believe a new build would solve your problem. Please follow these steps if not:

我相信新的版本会解决你的问题。如果没有,请遵循以下步骤:

  • Step 1: add this into BundleConfig.cs

    步骤1:将此添加到BundleConfig.cs中

    bundles.Add(new ScriptBundle("~/bundles/common").Include("~/Scripts/common.js"));

    包。添加(新ScriptBundle(~ /包/普通)其中包括(~ /脚本/ common.js "));

  • Step 2: add this line in your view/layout

    步骤2:在视图/布局中添加这一行

    @Scripts.Render("~/bundles/common")

    @Scripts.Render(“~ /包/普通”)

  • Step 3: build your project and run it.

    步骤3:构建项目并运行它。