MVC 4 @Scripts “does not exist”

后端 未结 24 1940
长发绾君心
长发绾君心 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 11:57

    I had this issue after I added an Area to a project that didn't have any. To get rid of it just copied the web.config withing root Views folder to the Views folder of the area and it started working.

    0 讨论(0)
  • 2020-12-12 11:57

    If you added to your web.config and it still shows message, then you need to close your project and reopen it, now it will exist and @Styles.Render("") and @Scripts.Render() will work fine.

    0 讨论(0)
  • 2020-12-12 11:58

    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.

    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:

    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:

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

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

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

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

        <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" />
    
    0 讨论(0)
  • 2020-12-12 12:00

    Just write

    @section Scripts{
        <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>
    }
    
    0 讨论(0)
  • 2020-12-12 12:02

    That has an obvious solution. I had the same problem later. Not related to Assembly References or ... .It'll occur In hierarchy calling of MVC Partial views, when you have complicated page structures. So calling/rendering each part separately on each page (maybe a master page or partial) will cause to not see required parts of page like the bellow code :

    @RenderSection("Scripts", required: false)
    

    That simply forces page to find and render related section and in case of failure shows you an error message like you.

    So I suggest you to trace your pages (like program trace) from master to all of its partials to Detect Dependencies. Maybe it be a terrible work, but no other choices available here.

    Not that according to my experience, some conditional situations in programming causes not to show you the right error causes the problem.

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