简体   繁体   中英

MVC5 with VB.NET: “BC30451: 'ViewData' is not declared.” when switching to Debug configuration

I have a MVC5 project that is currently set on the "Release Configuration" and it works 100%. However, as soon as I switch the project configuration from Release to Debug, then everything goes wrong... even if I switch it back to Release mode, everything is still broken. Only way I can get the project working again is to restore from a backup.

Here are what is happening. Firstly, when running the project, I get the following error:

BC30451: 'ViewData' is not declared. It may be inaccessible due to its protection level.

If I open any view in the project with Visual Studio 2013, I can see that all sorts of things are marked as errors like ViewData, Html, Url, etc.

When referring to @Html or @ViewData in the view, it normally refers to the .Html and .ViewData properties of the view's base class (WebViewPage). However, if I start typing "@Html." in any of the views, I can see in the autocomplete that it is referring to the System.Web.Webpages.Html namespace instead of the WebViewPage.Html property. It is as-if the view isn't inheriting from the System.Web.Mvc.WebViewPage class.

Any guidance as to where I can start looking to get this fixed or why this is happening?

So since nobody responded, I went through the long way.所以由于没有人回应,我走了很长的路。 I created a brand new MVC5 project, added all the packages via Nuget and then simply copied all my files over from the old project to the new one and now it works.

Does anybody have any idea what the heck could be causing this? I don't want to go through all this trouble again in the future if the project again suddenly decides to stop working.

Ok, I think I've found part of the cause here. Thing is, since the original code is valid (it compiles correctly and intellisense picks it up) and the code used to work and then all of a sudden, one day after compiling, it just stops working.

Anyway, in the view, when specifying the ModelType, if you don't use the full name, this error can occur or occurs eventually. For example, using:

@ModelType Models.SomeNamespace.SomeClass

will cause the error (even though the Root Namespace for the project is "MyProject") and it can be fixed by simply specifying the full namespace and class name.

@ModelType MyProject.Models.SomeNamespace.SomeClass

It's possible that a debug assembly is locked. Can close Visual Studio, search and delete for all bin folders in the solution directory. Then open and rebuild.

The other options is to go into the MVC project properties and compare the two build configurations. Are you targeting a different .NET framework between Release and Debug? 32 bit vs 64 bit? etc?

Looks like some MVC assemblies are corrupted. Have you checked your referenced assemblies for some errors?

Try also cleaning all the .NET internal cache. More in this stackoverflow topic: Could not load file or assembly ... The parameter is incorrect

在我的情况下,导入的 ViewModel 类名不正确,纠正它可以解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM