asp.net-mvc MVC 4 @Scripts“不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:00:01  来源:igfitidea点击:

MVC 4 @Scripts "does not exist"

asp.net-mvcrazor

提问by FantasticJamieBurns

I have just created an ASP.NET MVC 4 project and used Visual Studio 2012 RC to create a Controller and Razor Views for Indexand CreateActions.

我刚刚创建了一个 ASP.NET MVC 4 项目,并使用 Visual Studio 2012 RC 为IndexCreate操作创建了控制器和 Razor 视图。

When I came to run the application, and browsed to the Createview, the following error was shown:

当我开始运行应用程序并浏览到Create视图时,显示以下错误:

Compiler Error Message: CS0103: The name 'Scripts' does not exist in the current context

编译器错误消息:CS0103:当前上下文中不存在名称“脚本”

The problem is the following code which was added automatically to the bottom of the View:

问题是以下代码自动添加到视图底部:

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

Why does Scripts not exist?

为什么脚本不存在?

I looked at the base Web Page class in Assembly System.Web.Mvc.dll, v4.0.0.0

我查看了 Assembly System.Web.Mvc.dll, v4.0.0.0 中的基本网页类

I can see the following helper properties available:

我可以看到以下可用的辅助属性:

  • Ajax
  • Html
  • Url
  • 阿贾克斯
  • html
  • 网址

But nothing named Scripts.

但没有命名脚本。

Any ideas?

有任何想法吗?

EDIT:

编辑:

My Web.config file looks like this (untouched from the one that Visual Studio created):

我的 Web.config 文件看起来像这样(与 Visual Studio 创建的文件没有任何关系):

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

EDIT #2:

编辑#2:

People are blogging about using the @Scriptshelper:

人们正在写关于使用@Scripts助手的博客:

Yet having just installed Visual Studio 2012 RC onto a fresh Windows 8 install I am still unable to use @Scriptseven though Visual Studio adds it to the generated View!

然而,刚刚将 Visual Studio 2012 RC 安装到全新的 Windows 8 安装上,@Scripts即使 Visual Studio 将其添加到生成的视图中,我仍然无法使用!

Solutions are presented below.

解决方案如下。

I am not sure how to close this, because in the end an update seemed to resolve the issue. I double checked I had performed a clean install, using a new project. But the same failing project I had made works fine now after various updates and no manual obvious intervention. Thanks for all of the thoughts but there was definitely an issue at the time ;)

我不知道如何关闭它,因为最终更新似乎解决了这个问题。我仔细检查了我是否使用新项目执行了全新安装。但是,经过各种更新并且没有明显的手动干预,我所做的同样失败的项目现在可以正常工作了。感谢您的所有想法,但当时肯定有问题;)

回答by Marko

The key here is to add

这里的关键是添加

 <add namespace="System.Web.Optimization" /> 

to BOTH web.config files. My scenario was that I had System.Web.Optimization reference in both project and the main/root web.config but @Scripts still didn't work properly. You need to add the namespace reference to the Views web.config file to make it work.

到两个 web.config 文件。我的情况是我在项目和主/根 web.config 中都有 System.Web.Optimization 参考,但 @Scripts 仍然无法正常工作。您需要将命名空间引用添加到 Views web.config 文件以使其工作。

UPDATE:

更新:

Since the release of MVC 4 System.Web.Optimization is now obsolete. If you're starting with a blank solution you will need to install the following nuget package:

自 MVC 4 System.Web.Optimization 发布以来,现在已经过时了。如果您从空白解决方案开始,则需要安装以下 nuget 包:

Install-Package Microsoft.AspNet.Web.Optimization

You will still need to reference System.Web.Optimization in your web.config files. For more information see this topic:

您仍然需要在 web.config 文件中引用 System.Web.Optimization。有关更多信息,请参阅此主题:

How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

如何为 MVC-3-converted-to-4 应用程序添加对 System.Web.Optimization 的引用

As many pointed out, restart of VScould be required after the above steps to make this work.

正如许多人指出的那样,在上述步骤之后可能需要重新启动 VS才能完成这项工作。

回答by VJAI

@Stylesand @Scriptsare 2 new helpers provided by System.Web.Optimizationlibrary. As the name suggests, they bundle and minify CSS and JavaScript files or resources respectively.

@Styles并且@ScriptsSystem.Web.Optimization图书馆提供的 2 个新助手。顾名思义,它们分别捆绑和缩小 CSS 和 JavaScript 文件或资源。

Try including the namespace System.Web.Optimizationeither by @usingdirective or through web.config

尝试System.Web.Optimization通过@using指令或通过包含命名空间web.config

http://ofps.oreilly.com/titles/9781449320317/ch_ClientOptimization.html#BundlingAndMinification

http://ofps.oreilly.com/titles/9781449320317/ch_ClientOptimization.html#BundlingAndMinification

UPDATE

更新

Microsoft has moved the bundling/minification to a separate package called Microsoft.AspNet.Web.Optimization. You can download the assembly from nuget.

微软已将捆绑/缩小移动到一个名为Microsoft.AspNet.Web.Optimization. 您可以从nuget下载程序集。

This postwill be useful to you.

这篇文章会对你有用。

回答by Ben

There was one small step missing from the above, which I found on another post. After adding

上面遗漏了一小步,这是我在另一篇文章中找到的。添加后

<add namespace="System.Web.Optimization" />

to your ~/Views/web.confignamespaces, close and re-open Visual Studio.This is what I had to do to get this working.

到您的~/Views/web.config命名空间,关闭并重新打开 Visual Studio。这就是我必须做的才能让它发挥作用。

回答by N40JPJ

I am using areas, and have just come up against this issue, I just copied the namespaces from the root web.config to the areas web. config and it now works!!

我正在使用区域,并且刚刚遇到这个问题,我只是将命名空间从根 web.config 复制到区域 web。配置,现在可以工作了!!

    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />        
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />

回答by Dan Friedman

I had the same problem and I used WinMerge to help me track this down. But as I researched it more, I found that Rick has the perfect blog postfor it.

我遇到了同样的问题,我使用 WinMerge 来帮助我追踪这个问题。但随着我对其进行更多研究,我发现 Rick 拥有完美的博客文章

Summary:

概括:

  • Add <add namespace="System.Web.Optimization"/>to both web.config files
  • Run Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization
  • Update Bundling code
  • Update Layout file
  • The last step is to update 10 other libraries. I didn't and it worked fine. So looks like you can procrastinate this one (unless I already updated 1 or more of them). :)
  • 添加<add namespace="System.Web.Optimization"/>到两个 web.config 文件
  • Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization
  • 更新捆绑代码
  • 更新布局文件
  • 最后一步是更新其他 10 个库。我没有,而且效果很好。所以看起来你可以拖延这个(除非我已经更新了其中的一个或多个)。:)

回答by WWC

I had the same issue:

我遇到过同样的问题:

The System.Web.Optimization version I was using was outdated for MVC4 RC.

我使用的 System.Web.Optimization 版本对于 MVC4 RC 来说已经过时了。

I updated my packages using the package manager in VS 2010.

我使用 VS 2010 中的包管理器更新了我的包。

In this MSDN blog, Mr. Andy talks about how to update your MVC 4 Beta project to MVC 4 RC. Updating my packages got the Scripts (particularly the web optimization one) to resolve for me:

在这篇MSDN 博客中,Andy 先生谈论了如何将您的 MVC 4 Beta 项目更新到 MVC 4 RC。更新我的包得到了脚本(特别是网络优化)来为我解决:

To install the latest System.Web.Optimization package, use Package Manager Console (Tools -> Library Package Manager -> Package Manager Console) and run the below command:

要安装最新的 System.Web.Optimization 包,请使用包管理器控制台(工具 -> 库包管理器 -> 包管理器控制台)并运行以下命令:

Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization

Use the System.Web.Optimization file included in the package in your references.

在您的参考资料中使用包中包含的 System.Web.Optimization 文件。

To update other packages: Tools menu -> Library Package Manager -> Manage NuGet Packages for Solution.

更新其他包:工具菜单 -> 库包管理器 -> 管理解决方案的 NuGet 包。

回答by RickAndMSFT

Create a new MVC 4 RC internet application and run it. Navigate to Login which uses the same code

创建一个新的 MVC 4 RC Internet 应用程序并运行它。导航到使用相同代码的登录

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

What allows Login.cshtml to work is the the Views\Web.config file (not the app root version) contains

允许 Login.cshtml 工作的是 Views\Web.config 文件(不是应用程序根版本)包含

<namespaces>

    <add namespace="System.Web.Optimization"/>

  </namespaces>

Why is your Create view not working and Login is?

为什么您的创建视图不起作用而登录是?

回答by xAditya3393

Import System.Web.Optimization on top of your razor view as follows:

在您的剃刀视图之上导入 System.Web.Optimization,如下所示:

@using System.Web.Optimization

回答by Jake1164

I ran into this problem, however while running the command:

我遇到了这个问题,但是在运行命令时:

Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization 

I received the cryptic message (gotta love a great pun before the first cup of coffee):

我收到了一条神秘的信息(在第一杯咖啡之前一定要喜欢一个很棒的双关语):

Install-Package : The specified cryptographic algorithm is not supported on this platform.

Install-Package :此平台不支持指定的加密算法。

I am running this on Windows XP SP3 (not by choice) and what I found was that I had to follow the instructions posted by the user artsnobon the ASP.NET Forum

我在 Windows XP SP3 上运行它(不是选择),我发现我必须按照用户artnobASP.NET 论坛上发布的说明进行操作

  • Please uninstall the Nuget and try re-installing it. If you are unable to do this, login as an Administrator.
  • Go to Tools=> Extension Manager => Select "Nuget Package Manager" => UnInstall
  • Install it again, by searching "Nuget" => Install.
  • If it did not work, please try installing, 1.7.x version as I mentioned in the previous post (It doesn't mean, you have to use the previous version, if it works fine, we can report this bug, and get the patches for the latest version).
  • 请卸载 Nuget 并尝试重新安装。如果您无法执行此操作,请以管理员身份登录。
  • 转到工具 => 扩展管理器 => 选择“Nuget 包管理器” => 卸载
  • 通过搜索“Nuget”=> 安装,再次安装它。
  • 如果还是不行,请尝试安装,1.7.x版本我在上一篇文章中提到过(这并不意味着你必须使用以前的版本,如果它工作正常,我们可以报告这个错误,并得到最新版本的补丁)。

Once I ran this I could then run the command line to update the Web.Optimization.

一旦我运行了它,我就可以运行命令行来更新 Web.Optimization。

Hope this saves someone some digging.

希望这可以节省一些挖掘工作。

回答by archil

Just write

写就好了

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