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

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

问题描述

我刚刚创建了一个ASP.NET MVC 4项目和使用的Visual Studio 2012 RC创建一个控制器和视图剃刀索引和创建操作。

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.

当我来到运行应用程序,并浏览到创建视图,显示出以下错误:

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

问题是这是自动添加到视图底部以下code:

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

Line 32: 
Line 33: @section Scripts {
Line 34:     @Scripts.Render("~/bundles/jqueryval")
Line 35: }

为什么脚本不存在?

Why does Scripts not exist?

我看着在大会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:

<一个href=\"http://www.hanselman.com/blog/VisualStudio2012RCIsReleasedTheBigWebRollup.aspx\">http://www.hanselman.com/blog/VisualStudio2012RCIsReleasedTheBigWebRollup.aspx

<一个href=\"http://$c$cbetter.com/howarddierking/2012/06/04/web-optimization-in-visual-studio-2012-rc\">http://$c$cbetter.com/howarddierking/2012/06/04/web-optimization-in-visual-studio-2012-rc

然而,刚刚安装了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!

解决方案都低于psented $ P $。

我不知道如何关闭这一点,因为最终的更新似乎解决该问题。我仔细检查了我已经完成一个干净的安装,采用了新的项目。但是,我做了同样的失败项目的各种更新,无需人工干预明显现在后工作正常。感谢所有的想法,但也肯定是当时的一个问题;)

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

要BOTH web.config文件中。我的情况是,我不得不在这两个项目System.Web.Optimization参考,主/根web.config但@Scripts仍然没有正常工作。您需要添加命名空间参考意见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

您仍然需要参考System.Web.Optimization在你的web.config文件。欲了解更多信息,请参阅本主题:

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

<一个href=\"http://stackoverflow.com/questions/9475893/how-to-add-reference-to-system-web-optimization-for-mvc-3-converted-to-4-app\">How添加引用System.Web.Optimization对MVC-3转换到4应用

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

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