Razor Views not seeing System.Web.Mvc.HtmlHelper

后端 未结 22 954
我在风中等你
我在风中等你 2020-11-27 05:27

I am in the process of upgrading to MVC4. I have followed the instructions at http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 but in my Razor views and layou

相关标签:
22条回答
  • 2020-11-27 05:50

    I ran into this issue with a Web Application - my .cshtml files got stuck on the System.Web.WebPages.WebViewPage base class when I needed the System.Web.Mvc.WebViewPage.

    First, ensure your ~/Views/web.config file has the correct pageBaseType. In my case, I set to System.Web.Mvc.WebViewPage.

    <configuration>
      <system.web.webPages.razor>
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <!-- ... -->
        </pages>
      </system.web.webPages.razor>
    </configuration>
    

    Then, importantly, some people have found this is key if the above is already good:

    1. Run a clean on the solution
    2. Unload the project with the issues
    3. Delete the .user file that Visual Studio generated next to the project.
    4. Reload the project with the issues
    5. Build the solution

    For VS2015, the .user and .sln files have moved to the .vs hidden folder that is created next to the .sln file. However, from comments below, the error messages imply to me that the tooling is using the wrong version of MVC entirely, and deleting this folder does not fix the issue. To my knowledge, there is not a known solution.

    0 讨论(0)
  • 2020-11-27 05:50

    In the contracting world I'm often using machines that are using older images. After trying everything above I decided to update my VS 2013 to the most recent version (Update 4). After 90 minutes and a restart the references are working just fine now! Hope this helps!

    0 讨论(0)
  • 2020-11-27 05:53

    I used the following article on MSDN to solve this issue (in this case from MVC 4 to MVC 5)

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

    Main extracts from the article are:

    Main Web.config (Not the one in the Views folder)

    In the runtime block update System.Web.Mvc to 5.0.0.0 ,System.Web.Helpers to 3.0.0.0 and System.Web.WebPages to 3.0.0.0

    <dependentAssembly>
      <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
    </dependentAssembly>
    
    <dependentAssembly>
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    

    Then look for the appsettings block and update the webpages:Version value to 3.0.0.0

    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
    </appSettings>
    

    Views Web.config

    Update the host factoryType block to System.Web.Mvc to 5.0.0.0

    <system.web.webPages.razor>
       <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
             <add namespace="System.Web.Mvc" />
          </namespaces>
       </pages>
    </system.web.webPages.razor>
    

    Then under the pages block update any references to System.Web.Mvc to 5.0.0.0

        <pages
          validateRequest="false"
          pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <controls>
          <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
        </controls>
      </pages>
    

    And finally under the configuration block update the System.Web references to 3.0.0.0

    <configuration>
      <configSections>
        <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">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
      </configSections>
    
    0 讨论(0)
  • Just started looking into the issue myself and this is what it look like in my case. If you have the correct values in your web config then Its just a bug in MVC4. http://connect.microsoft.com/VisualStudio/feedback/details/727729/viewbag-not-recognized-in-asp-net-mvc-4-project

    0 讨论(0)
  • 2020-11-27 05:55

    Update for Visual Studio 2017 Users:

    If you have just migrated to Visual Studio 2017 and your project is MVC4, make sure you go back into the VS2017 Installer and check the MVC4 option under the "ASP.NET and web development" section. It is an optional component and not checked by default.

    This solved my issue

    0 讨论(0)
  • 2020-11-27 05:57

    Recently got the same problem and this is how i fixed it: On Visual Studio with your project in question open, Goto:

    1. Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.
    2. On the open window, select Updates. then Click Update All.

    It will load whats missing in your project and all should be back on track.

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