什么在MVC 6更换为@ Scripts.Render [英] Whats the replacement for @Scripts.Render in MVC 6

查看:214
本文介绍了什么在MVC 6更换为@ Scripts.Render的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的MVC 6运行一些d3js,并期待在这个例子<一个href=\"https://github.com/DustinEwers/D3-DotNetMVC-Demos/blob/master/D3Demos/Views/TagDemos/BasicBarChart.cshtml\">https://github.com/DustinEwers/D3-DotNetMVC-Demos/blob/master/D3Demos/Views/TagDemos/BasicBarChart.cshtml并使用

I am trying to run some d3js on my MVC 6 and was looking at this example https://github.com/DustinEwers/D3-DotNetMVC-Demos/blob/master/D3Demos/Views/TagDemos/BasicBarChart.cshtml and it uses

@Scripts.Render("~/bundles/d3")
@section Scripts{

但是,如果我这样做,我得到

But if I do that I get

名称'脚本'不在当前上下文中存在

The name 'Scripts' does not exist in the current context

那么,有没有一种新的方式做在MVC 6?

So is there a new way to do it in MVC 6 ?

推荐答案

在ASP.NET 5,有没有这样的 Scripts.Render 方法。要渲染的脚本,您可以使用环境标签帮手。

In ASP.NET 5, There is no such Scripts.Render method. To render scripts, you may use the environment tag helper.

这是没有必要,你应该使用环境标签帮手。您可以直接把你的脚本标签在布局文件。但环境佣工使我们有条件地呈现基于对环境的脚本。 (精缩捆绑版VS联合国所有精缩版的)

It is not necessary that you should use the environment tag helper. You can directly put your script tags in the layout file. But the environment helpers allows us to conditionally render scripts based on the environment. (Minified-Bundled version vs All Un minified version)

下面是语法,您可以包括在此布局的文件。

Here is the syntax, you can include this in your Layout file.

<environment names="Development">
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/js/d3.js"></script>
</environment>
<environment names="Staging,Production">
    <script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
            asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
            asp-fallback-test="window.jQuery">
    </script>            
    <script src="~/js/d3.min.js" asp-file-version="true"></script>
</environment>

假设你有脚本文件 d3.js 和d3.min.js 在<$ C $存在C>〜/ JS 目录

此外,你需要确保你已经调用了配置()方法中的 UseStaticFiles()方法(里面的 Startup.cs 的)

Also you need to make sure that you have invoked the UseStaticFiles() method inside the Configure() method(inside Startup.cs)

public void Configure(IApplicationBuilder app, IHostingEnvironment env,  
                                                             ILoggerFactory loggerFactory)
{
     //Other configuration goes here
     app.UseStaticFiles();  // This enables static file serving from the app.
     app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
}

UseStaticFiles()扩展方法,使静态文件服务,包括js文件,css文件等。

UseStaticFiles() extension method enables static file serving including js files,css files etc..

当你运行在开发模式下的应用,这将使内部环境发展脚本标记,当你在任何临时或生产运行它,它会呈现在运行,生产环境脚本标签。

When you run the application in Development mode, It will render the script tags inside environment "Development" and when you run it in either Staging or Production, It will render script tags under the "Staging,Production" environment.

您可以通过右击该项目改变环境值,然后选择属性 - &GT;调试并指定环境变量的值主机:环境

You can change the environment value by right clicking on the project and select properties->Debug and specify the value of the environment variable Hosting:Environment

您可以看到,我已包括的js文件在临时/生产enviornment缩小的版本。这是没有必要的,但preferred方法,因为它会节省一些带宽。 (你可以把非精缩版也有,而不是缩小的,如果你真的想这样做。的)。如果你有一个单一的捆绑文件,您可以使用也在这里,而不是单独的文件。

You can see that i have included the minified version of js files in the Staging/Production enviornment. This is not necessary but preferred approach as it will save some bandwidth. (You can put the un-minified version also there instead of minified if you really want to do that.). If you have a single bundled file, you may use that also here instead of individual files.

如果你已经没有一个缩小的版本,你可以通过运行一饮而尽任务微小产生。(这是包含在默认 gulp.js 文件,该文件在新的Web应用程序模板)。你只需要打开任务亚军和运行微小任务。

If you already do not have a minified version, you can generate that by running the gulp task for minification.(This is included in the default gulp.js file which is in the new web app template). You just need to open up the task runner and run the minification task.

在这里输入的形象描述

如果您不希望每次都手动运行,您可以选择绑定 - &GT;构建之前所以,这将自动运行purticular一饮而尽任务每次你建立你的项目。

If you do not wish to manually run this every time, You may select Bindings -> Before build so that this will automatically run that purticular gulp task everytime you build your project.

这篇关于什么在MVC 6更换为@ Scripts.Render的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆