IT story

MVC 4 @Scripts“존재하지 않습니다”

hot-time 2020. 5. 8. 08:24
반응형

MVC 4 @Scripts“존재하지 않습니다”


방금 ASP.NET MVC 4 프로젝트를 만들고 Visual Studio 2012 RC를 사용하여 컨트롤러 및 면도기보기 IndexCreate작업 을 만들었습니다 .

응용 프로그램을 실행하고 Create보기를 탐색 할 때 다음 오류가 표시되었습니다.

컴파일러 오류 메시지 : CS0103 : 현재 컨텍스트에 'Scripts'라는 이름이 없습니다.

문제는 View의 맨 아래에 자동으로 추가 된 다음 코드입니다.

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

왜 스크립트가 존재하지 않습니까?

Assembly System.Web.Mvc.dll, v4.0.0.0의 기본 웹 페이지 클래스를 보았습니다.

다음과 같은 도우미 속성을 사용할 수 있습니다.

  • 아약스
  • HTML
  • URL

그러나 스크립트라는 이름은 없습니다.

어떤 아이디어?

편집하다:

내 Web.config 파일은 다음과 같습니다 (Visual Studio에서 만든 파일에서 변경되지 않음).

  <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도우미 사용에 대한 블로그를 작성하고 있습니다 .

그러나 새로운 Windows 8 설치에 Visual Studio 2012 RC를 방금 설치 @Scripts했지만 Visual Studio가 생성 된 View에 추가하더라도 여전히 사용할 수 없습니다 !

솔루션은 다음과 같습니다.

결국 업데이트가 문제를 해결하는 것처럼 보였으므로 닫는 방법을 잘 모르겠습니다. 새 프로젝트를 사용하여 새로 설치를 수행했는지 다시 확인했습니다. 그러나 내가 만든 동일한 실패한 프로젝트는 다양한 업데이트와 수동 개입 없이도 잘 작동합니다. 모든 생각에 감사하지만 당시에는 분명히 문제가있었습니다.)


여기서 핵심은 추가하는 것입니다

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

web.config 파일 모두에. 내 시나리오는 프로젝트와 기본 / 루트 web.config 모두에서 System.Web.Optimization 참조를 가지고 있었지만 @Scripts는 여전히 제대로 작동하지 않았습니다. 네임 스페이스 참조를 Views web.config 파일에 추가해야 작동합니다.

최신 정보:

MVC 4 System.Web.Optimization의 출시 이후 더 이상 사용되지 않습니다. 빈 솔루션으로 시작하는 경우 다음 nuget 패키지를 설치해야합니다.

Install-Package Microsoft.AspNet.Web.Optimization

web.config 파일에서 여전히 System.Web.Optimization을 참조해야합니다. 자세한 정보는 다음 주제를 참조하십시오.

MVC-3-converted-to-4 앱에 대한 System.Web.Optimization에 대한 참조를 추가하는 방법

많은 사람들이 지적했듯이 위의 단계 후에 VS를 다시 시작 해야이 작업을 수행 할 수 있습니다.


@Styles@Scripts에서 제공하는 2 개의 새로운 도우미입니다 System.Web.Optimization라이브러리. 이름에서 알 수 있듯이 CSS와 JavaScript 파일 또는 리소스를 각각 묶고 축소합니다.

지시문 또는 다음을 통해 네임 스페이스 System.Web.Optimization포함 시키십시오.@usingweb.config

http://ofps.oreilly.com/titles/9781449320317/ch_ClientOptimization.html#BundlingAndMinification

최신 정보

Microsoft는 번들링 / 축소를라는 별도의 패키지로 옮겼습니다 Microsoft.AspNet.Web.Optimization. nuget 에서 어셈블리를 다운로드 할 수 있습니다 .

게시물 이 도움이 될 것입니다.


위에서 다른 작은 단계가 누락되어 다른 게시물에서 발견했습니다. 추가 후

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

당신에 ~/Views/web.config네임 스페이스, 닫고 다시 Visual Studio를 엽니 다. 이것이 내가이 일을하기 위해해야했던 일입니다.


영역을 사용하고 있으며이 문제와 관련하여 방금 네임 스페이스를 루트 web.config에서 영역 웹으로 복사했습니다. 구성 및 이제 작동합니다!

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

나는 같은 문제가 있었고 WinMerge를 사용하여 이것을 추적하는 데 도움을주었습니다. 그러나 더 자세히 조사한 결과 Rick이 블로그에 대한 완벽한 블로그 게시물가지고 있음을 발견 했습니다.

요약:

  • <add namespace="System.Web.Optimization"/>두 web.config 파일 모두에 추가
  • 운영 Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization
  • 번들링 코드 업데이트
  • 레이아웃 파일 업데이트
  • 마지막 단계는 10 개의 다른 라이브러리를 업데이트하는 것입니다. 나는하지 않고 잘 작동했습니다. 따라서이 중 하나를 이미 업데이트하지 않은 한이를 미루는 것처럼 보입니다. :)

나는 같은 문제가 있었다.

내가 사용하고 있던 System.Web.Optimization 버전은 MVC4 RC 용으로 구 버전입니다.

VS 2010에서 패키지 관리자를 사용하여 패키지를 업데이트했습니다.

In this MSDN blog, Mr. Andy talks about how to update your MVC 4 Beta project to MVC 4 RC. Updating my packages got the Scripts (particularly the web optimization one) to resolve for me:

To install the latest System.Web.Optimization package, use Package Manager Console (Tools -> Library Package Manager -> Package Manager Console) and run the below command:

Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization

Use the System.Web.Optimization file included in the package in your references.

To update other packages: Tools menu -> Library Package Manager -> Manage NuGet Packages for Solution.


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?


Import System.Web.Optimization on top of your razor view as follows:

@using System.Web.Optimization

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.


Just write

@section Scripts{
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>
}

I upgraded from Beta to RC and faced 'Scripts' does not exist issue. Surfed all over the web and the final solution is what N40JPJ said plus another workaroudn:

Copy the following in View\Web.config :

  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Helpers"/>
  </namespaces>

and the following inside View\Shared_Layout.cshtml

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

Hope it helps.


  1. : remove view page.
  2. : add namespace webconfig (in view directory)
  3. : create view an try!

good luck...


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.


just remove/ hide the code from create & Edit razor view of your controller.

 @section Scripts {  

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

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!


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.


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.


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.


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.


For me this solved the problem, in NuGet package manager console write following:

update-package microsoft.aspnet.mvc -reinstall

When i started using MVC4 recently i faced the above issue while creating a project with the empty templates. Steps to fix the issue.

  1. Goto TOOLS --> Library Package Manager --> Packager Manager Console Paste the below command and press enter Install-Package Microsoft.AspNet.Web.Optimization Note: wait for successful installation.
  2. Goto Web.Config file in root level and add below namespace in pages namespace section. add <namespace="System.Web.Optimization" />
  3. Goto Web.Config in Views folder and follow the step 2.
  4. Build the solution and run.

The Package mentioned in step 1 will add few system libraries into the solution references like System.Web.Optimization is not a default reference for empty templates in MVC4.

I hope this helps. Thank you


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.


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.


Try this:

@section Scripts 
{
    Scripts.Render("~/bundles/jqueryval")  // <- without ampersand at the begin
}

참고URL : https://stackoverflow.com/questions/10951669/mvc-4-scripts-does-not-exist

반응형