MVC 4 @Scripts “does not exist”

2019-01-20 23:42发布

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:

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")
}

Why does Scripts not exist?

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:

  • Ajax
  • Html
  • Url

But nothing named Scripts.

Any ideas?

EDIT:

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>

EDIT #2:

People are blogging about using the @Scripts helper:

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!

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

24条回答
We Are One
2楼-- · 2019-01-21 00:09

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.

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 artsnob on the ASP.NET Forum

  • 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).

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

Hope this saves someone some digging.

查看更多
再贱就再见
3楼-- · 2019-01-21 00:09

When I enter on a page that haves this code:

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

This error occurs: Error. An error occurred while processing your request.

And this exception are recorded on my logs:

System.Web.HttpException (0x80004005): The controller for path '/bundles/jqueryval' was not found or does not implement IController.
   em System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
...

I have tried all tips on this page and none of them solved for me. So I have looked on my Packages folder and noticed that I have two versions for System.Web.Optmization.dll:

  • Microsoft.AspNet.Web.Optimization.1.1.0 (v1.1.30515.0 - 68,7KB)
  • Microsoft.Web.Optimization.1.0.0-beta (v1.0.0.0 - 304KB)

My project was referencing to the older beta version. I only changed the reference to the newer version (69KB) and eveything worked fine.

I think it might help someone.

查看更多
趁早两清
4楼-- · 2019-01-21 00:10

Try this:

@section Scripts 
{
    Scripts.Render("~/bundles/jqueryval")  // <- without ampersand at the begin
}
查看更多
疯言疯语
5楼-- · 2019-01-21 00:11

Apparently you have created an 'Empty' project type without 'Scripts' folder. My advice -create a 'Basic' project type with full 'Scripts' folder.

With respect to all developers.

查看更多
Emotional °昔
6楼-- · 2019-01-21 00:13

One more for the pot - spent ages trying to work out the same problem - even though it was defined in the web.config for root and the root of Views. Turns out I'd mistakenly added it to the <system.web><pages><namespaces>, and not <system.web**.webPages.razor**><pages><namespaces> element.

Really easy to miss that!

查看更多
欢心
7楼-- · 2019-01-21 00:13

I solve this problem in MvcMusicStore by add this part of code in _Layout.cshtml

@if (IsSectionDefined("scripts")) 
{
       @RenderSection("scripts", required: false)
}

and remove this code from Edit.cshtml

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

Run the program inshallah will work with you.

查看更多
登录 后发表回答