Programing

현재 컨텍스트에 'ViewBag'이름이 없습니다.-Visual Studio 2015

crosscheck 2020. 9. 16. 07:08
반응형

현재 컨텍스트에 'ViewBag'이름이 없습니다.-Visual Studio 2015


ASP.NET에서 다시 개발하기 시작했는데 Visual Studio에서 작은 오류가 발생했습니다. 내 .cshtml 파일은 몇 가지 면도기 기능을 사용할 때 오류를 표시합니다. 예 : "현재 컨텍스트에 'ViewBag'이름이 없습니다." 다음은 사진입니다.

Visual Studio 2015의 문제 스크린 샷

데모 프로젝트를 사용하고 있습니다. 프로젝트는 https://github.com/Wintellect/Angular-MVC-Cookbook/tree/master/BasicProject 에서 찾을 수 있습니다.

몇 가지 다른 스레드를 살펴 보았으며 대부분 web.config 파일을 업데이트하는 것이 좋습니다. 이 2 개의 구성 파일은 이미 존재하며 꽤 인기있는 데모이므로 필요한 모든 구성이 포함되어 있다고 가정합니다. 물론 이러한 구성 파일을 살펴 보았으며 실제로 제안 된 솔루션이 포함되어 있습니다.

기타 세부 사항:

  • 나는 이미 솔루션에서 정리 및 재구성을 사용했지만 아무것도 변경하지 않았습니다.
  • 완전히 새로운 MVC 프로젝트를 만들면 작동합니다.
  • 내 친구는 같은 문제가 있으며 우리 둘 다 VS 2015와 Windows 10을 사용합니다.
  • 여전히 응용 프로그램을 실행할 수 있으며 작동합니다.

미리 감사드립니다.


모든 올바른 구성에도 불구 하고이 문제가 발생했습니다.

Razor 뷰가 ViewBag, Model 및 HtmlHelpers를 인식하지 못하도록 구성 요소 캐시의 일부 잘못된 파일로 판명되었습니다 . 이러한 파일을 삭제하면 문제가 해결되었습니다 (다음에 Visual Studio를 열 때 이러한 파일의 좋은 버전이 생성되었습니다).

파일은 다음 위치에 있습니다.

%LOCALAPPDATA%\Microsoft\VisualStudio\14.0\ComponentModelCache

4 개의 파일을 모두 삭제합니다.

  • Microsoft.VisualStudio.Default.cache
  • Microsoft.VisualStudio.Default.catalogs
  • Microsoft.VisualStudio.Default.err
  • Microsoft.VisualStudio.Default.external

이후에 다른 여러 개발자 컴퓨터에서 동일한 문제를 보았으며이 수정으로 신속하게 해결됩니다.


web.configViews 폴더 파일 설정에서 버전 번호를 업데이트 합니다.

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

너겟을 통해 MVC 버전을 업데이트 한 경우 다음과 같아야합니다.

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

다른 종속성 버전도 업데이트해야 할 수 있습니다. MVC 도우미 및 개체에 대한보기의 컴파일 및 네임 스페이스 문제는 일반적으로 web.config보기 폴더 내에서 엉망이 된 파일 과 관련이 있습니다.


appsettings에서 "webpages : Version"을 찾아 3.0.0.0 버전으로 업데이트합니다. 내 web.config에는

<add key="webpages:Version" value="2.0.0.0" />

그리고 나는 그것을 업데이트했습니다

<add key=”webpages:Version” value=”3.0.0.0″ />

web.config불필요하다고 생각하여 실수 로 View 폴더에서 제거 했습니다. 다시 넣으면 작동하기 시작했습니다.


태양 아래 모든 것을 시도 후에는 밝혀 뭔가 내 수정 한 System.Web.WebPages.Razor <sectionGroup>의를 /Views/Web.Config적절한 낙타 표기법에서 System.Web.WebPages.Razor모두 소문자로 system.web.webpages.razor궁극적으로 나의 죽음이었다.

이것이이 문제로 다른 불쌍한 영혼을 도울 수 있기를 바랍니다.

참고로 이것은 올바른 항목입니다 (내 시나리오의 경우).

<sectionGroup name="System.Web.WebPages.Razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 

나는이 문제에 더 많이 직면했고 마침내보기 폴더에 자체 웹 구성이 포함되어야하기 때문에 발생했다는 것을 발견했습니다.


때때로 web.config의 설정에서 버전 번호를 변경하는 것으로 충분하지 않습니다. 이 문제는 Visual Studio 2015에 mvc4 도구가 없기 때문에 발생합니다.

해결책은 프로젝트를 MVC 5로 업그레이드하는 것입니다. 문제가 완전히 해결됩니다. 이 페이지에서 도움을받을 수 있습니다. 그것은 그것을 명확하게 설명합니다.

http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web- api-2

그런데 Visual Studio 팀은 첫 번째 업데이트와 함께 Visual Studio 2015에 mvc4 지원을 추가 할 것이라고 선언했습니다.


나는 이것에 근무하는 하루 동안 잘 낭비했습니다. 나는이 (및 다른) 게시물의 모든 것을 시도했지만 아무 소용이 없었습니다. 결국 저는 다른 방식으로 프로젝트를 열어야한다는 것을 알게되었습니다. 방금 기존 웹 사이트를 Visual Studio 2015, MVC5, .NET 프레임 워크 4.5.2로 업그레이드했으며 Windows 10을 사용하고 있지만 각 소프트웨어 응용 프로그램의 다른 버전에 대한 대답이 동일했을 것이라고 생각합니다.

이것이 제가 웹 사이트를 열기로 선택한 메뉴 옵션입니다.

여기에 이미지 설명 입력

대신이 메뉴 옵션을 사용하여 프로젝트를 정의하는 csproj 파일을 열었습니다.

여기에 이미지 설명 입력

그런 다음 다른 많은 사람들이 언급하는 "깨끗한 솔루션"옵션을 사용했습니다.

여기에 이미지 설명 입력

However, I didn't even need this - everything just worked! Well, I got on to the next set of problems, anyway ...


late to the game but none of these solutions worked for me.

I upgraded to VS 2017, and the site worked, but Visual Studio compiler broke most of .cshtml files

TLDR;

mysolution.sln had the wrong version

sln diff

so you want to create a new solution with newer VS and compare it to yours and copy version info over.

full upgrade steps:

  1. ensure .sln file has correct version for VS
  2. right click MySolution in VS > Nuget Manager > Updates > run all updates
  3. right click MySolution in VS > Application > update Target Framework (4.5.2 in my case)
  4. fix any version issues in .config files (i.e. RAZOR to 3.0.0.0)
  5. fix any code issues in compiler (for me ForEach was ambiguous)

After having try at least 90% of every solution/trick of this subject (and others), I've finally solved it!! Spoiler alert, there is no trace of logic is the following so-called "solution": Running the solution with all tabs closed finally worked, when nothing else would have spare me the "ViewBag&Co does not exist in the current context"... You've been warned, no freaking logic!

Hope it'll help someone one day ^^


Turning it off and on again worked for me. You can try restarting visual studio.


I was able to resolve the issue by adding the following to the top of my .cshtml page

@{ViewBag.Title = "Title";}

After recompiling I was able to delete this line and the error was gone.


I had added the controller by

  • Step 1: Add > Class
  • Step 2: UserController
  • .
  • .
  • .

corresponding code created:

namespace SampleApp.Controllers
{
    public class UserController {
    }
}

My errors got changed by changing the Step 1 as:

  • Step 1: Add > Controller
  • Step 2: User Controller
  • .
  • .
  • .

corresponding code created:

namespace SampleApp.Controller {
    public class UserController : Controller {
    }
}

The problem was: I had not Inherited my class from Controller

Other messages in my error included - all of which were from this problem:

error CS0117: 'ModelState' does not contain a definition for 'IsValid'

error CS0103: The name 'RedirectToAction' does not exist in the current context

error CS0103: The name 'View' does not exist in the current context

error CS0103: The name 'ViewBag' does not exist in the current context

error CS0103: The name 'Json' does not exist in the current context

Hope this change helps those who have not paid attention in their initial steps.


나는 내 견해 중 하나와 비슷한 문제에 직면했다. 제 경우 에는 솔루션을 정리하고 다시 빌드 하려고 시도 했습니다 .


다음 링크가 도움이 될 수 있습니다.

문제가 무엇인지 보여줄 것입니다.

그것은 내 문제를 해결합니다. 어떻게 든보기 폴더에서 내 web.config 파일을 삭제했습니다.


다른 모든 것을 시도하고 약간 좌절감을 느낀 후 MVC5 앱의 .NET 프레임 워크를 4.5에서 4.5.2로 업그레이드했습니다. 어떻게 든 내 문제를 해결했습니다. 도움이되기를 바랍니다.


web.config에서 교체를 해결했습니다.

 <add key="webpages:Enabled" value="false" />

 <add key="webpages:Enabled" value="true" />

참고 URL : https://stackoverflow.com/questions/29319189/the-name-viewbag-does-not-exist-in-the-current-context-visual-studio-2015

반응형