MVC 4 @Scripts“不存在" [英] MVC 4 @Scripts "does not exist"

查看:34
本文介绍了MVC 4 @Scripts“不存在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

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

为什么脚本不存在?

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

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

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

I can see the following helper properties available:

  • 阿贾克斯
  • HTML
  • 网址

但没有命名脚本.

有什么想法吗?

我的 Web.config 文件如下所示(与 Visual Studio 创建的文件没有任何关系):

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

  <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>

编辑 #2:

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

People are blogging about using the @Scripts helper:

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

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

解决方案如下所示.

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

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 ;)

推荐答案

这里的关键是添加

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

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

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.

更新:

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

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:

Install-Package Microsoft.AspNet.Web.Optimization

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

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

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

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

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

这篇关于MVC 4 @Scripts“不存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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