如何将 @Scripts.Render
与 .NET Core 2.0 MVC 应用程序一起使用?
我正在将代码从 .NET Framework 4.6.1 转换为 .NET Core 2.0。我读过 here 如何与 .NET Core 2.0 bundle 在一起。如何修复错误,并用新版本替换代码?
代码:
@Scripts.Render("~/bundles/login")
它说
The name 'Scripts' does not exist in the current context
现有的BundleConfig.cs
:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryvalidate").Include(
BundleTable.EnableOptimizations = true;
}
最佳答案
在 ASP.Net MVC Core 中,他们删除了 BundleConfig.cs 并替换为 bundleconfig.json 文件。你需要在 bundleconfig.json 中指定你的包和缩小逻辑。如果您的项目中没有此文件,请添加具有此名称的 json 文件。
bundleconfig.json
此文件的内容应如下所示。
// Configure bundling and minification for the project.
// More info at https://go.microsoft.com/fwlink/?LinkId=808241
[
{
"outputFileName": "wwwroot/css/site.min.css",
// An array of relative input file paths. Globbing patterns supported
"inputFiles": [
"wwwroot/css/site.css"
]
},
{
"outputFileName": "wwwroot/js/bundles.min.js",
"inputFiles": [
"wwwroot/js/site.js",
"wwwroot/lib/jquery/dist/jquery.js",
"wwwroot/lib/jquery/dist/jqueryvalidate.js"
],
// Optionally specify minification options
"minify": {
"enabled": true,
"renameLocals": true
},
// Optionally generate .map file
"sourceMap": false
}
]
_Layout.cshtml
<script src="~/js/bundles.min.js"></script>
阅读与 Bundling and minification 相关的 Microsoft 文档进一步了解 asp.net core mvc 中的 bundle 和缩小
关于c# - 如何将 @Scripts.Render 与 .Net Core 2.0 MVC 应用程序一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415335/