MVC 4 @Scripts “does not exist”

后端 未结 24 1975
长发绾君心
长发绾君心 2020-12-12 11:20

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.

相关标签:
24条回答
  • 2020-12-12 12:20

    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.

    0 讨论(0)
  • 2020-12-12 12:20

    Try this:

    @section Scripts 
    {
        Scripts.Render("~/bundles/jqueryval")  // <- without ampersand at the begin
    }
    
    0 讨论(0)
  • 2020-12-12 12:21

    @Styles and @Scripts are 2 new helpers provided by System.Web.Optimization library. As the name suggests, they bundle and minify CSS and JavaScript files or resources respectively.

    Try including the namespace System.Web.Optimization either by @using directive or through web.config

    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.

    This post will be useful to you.

    0 讨论(0)
  • 2020-12-12 12:22

    I had a very similar error when upgrading a project from MVC3 to MVC4.

    Compiler Error Message: CS0103: The name [blah] does not exist in the current context

    In my case, I had outdated version numbers in several of my Web.Configs.

    1. I needed to update the System.Web.Mvc version number from "3.0.0.0" to "4.0.0.0" in every Web.Config in my project.
    2. I needed to update all of my System.Web.WebPages, System.Web.Helpers, and System.Web.Razor version numbers from "1.0.0.0" to "2.0.0.0" in every Web.Config in my project.

    Ex:

    <configSections>
      <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      </sectionGroup>
    </configSections>
    
    ...
    
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    

    Be sure to review the Web.Configs in each of your Views directories.

    You can read more about Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4.

    0 讨论(0)
  • 2020-12-12 12:23

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

     @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

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

    Why is your Create view not working and Login is?

    0 讨论(0)
  • 2020-12-12 12:23

    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!

    0 讨论(0)
提交回复
热议问题