2017-02-18 125 views
0

我在部署MVC VS2015模板創建一個MVC 5 Web應用程序「‘ViewBag’這個名字不會在目前情況下存在」。該應用程序裏工作大VS2015發佈然而之後(文件系統,然後複製)到Windows 2008 R2服務器/ IIS 7.5 - 將瀏覽器指向應用程序根給出了上面的錯誤。指着/帳號/登錄給出了略有不同:在服務器部署(MVC5/IIS 7.5)

"The name 'model' does not exist in the current context" 

我讀過很多答案對於這個錯誤,但沒有幫助,沒有具體針對我的ENV版本。香港專業教育學院已經嘗試過:

  • 部署到虛擬目錄
  • 部署到根目錄的空白網站
  • 添加
  • 驗證應用程序池.NET 4.0 /綜合
  • 添加web.config中視圖文件夾(默認情況下不創建MVC 5)

我錯過了什麼?

我得到(允許在web.config中詳細的錯誤信息之後)的錯誤:

我的錯誤:

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context 

Source Error: 


Line 1: @{ 
Line 2:  ViewBag.Title = "Home Page"; 
Line 3: } 
Line 4: 

我的web.config:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="ApplicationServices" connectionString="Data Source=win10nh\sqlexpress;Initial Catalog=*****;uid=sa;password=*****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /><add name="DefaultConnection" connectionString="Data Source=win10nh\sqlexpress;Initial Catalog=*****;uid=sa;password=*****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> 
    <add name="*****Entities1" connectionString="metadata=res://*/Models.EFmodel.Model1.csdl|res://*/Models.EFmodel.Model1.ssdl|res://*/Models.EFmodel.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=win10nh\sqlexpress;initial catalog=*****;user id=sa;password=*****;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /><add name="*****Entities" connectionString="metadata=res://*/Models.EFmodel.Model1.csdl|res://*/Models.EFmodel.Model1.ssdl|res://*/Models.EFmodel.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=win10dev\sqlexpress;initial catalog=*****;user id=sa;password=*****;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 

    <authentication mode="Forms"> 
    <!--http://stackoverflow.com/questions/2454623/multiple-applications-using-same-login-database-logging-each-other-out--> 
    <!--<forms timeout="20" loginUrl="~/Account/Login" domain="gc.*****.com" />--> 
    <forms timeout="60" loginUrl="~/Account/Login" /> 
    </authentication> 

    <membership> 
    <providers> 
     <clear /> 
     <add name="AspNetSqlMembershipProvider" connectionStringName="ApplicationServices" type="System.Web.Security.SqlMembershipProvider" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" /> 
    </providers> 
    </membership>  

    <roleManager enabled="true"> 
    <providers> 
     <clear /> 
     <add name="AspNetSqlRoleProvider" connectionStringName="ApplicationServices" type="System.Web.Security.SqlRoleProvider" applicationName="/" /> 
    </providers> 
    </roleManager> 

    <profile> 
     <providers> 
     <clear /> 
     <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /> 
     <!--<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="sqlServerMembership" />--> 
     </providers> 
     <properties> 
     <!--add name="UiCulture" serializeAs="String" allowAnonymous="true" />--> 
     </properties> 
    </profile> 


    <!--<authentication mode="Forms" />--> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 

    </system.web> 
    <system.webServer> 
    <modules> 
     <!--<remove name="FormsAuthentication" />--> 
     <!--<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />--> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    <validation validateIntegratedModeConfiguration="false" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <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.2.3.0" newVersion="5.2.3.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> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

回答

0

嘗試增加pages標籤配置文件中的system.web標籤。

<pages controlRenderingCompatibilityVersion="4.5"> 
    <namespaces> 
    <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" /> 
    </namespaces> 
</pages> 

同時檢查Views文件夾

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

    <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" /> 
     <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.Net.Http" /> 
     </namespaces> 
    </pages> 
    </system.web.webPages.razor> 

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

    <system.web> 
    <httpHandlers> 
     <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
    </httpHandlers> 

    <!-- 
     Enabling request validation in view pages would cause validation to occur 
     after the input has already been processed by the controller. By default 
     MVC performs request validation before a controller processes the input. 
     To change this behavior apply the ValidateInputAttribute to a 
     controller or action. 
    --> 
    <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> 
    </system.web> 

    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 

    <handlers> 
     <remove name="BlockViewHandler"/> 
     <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 
    </handlers> 
    </system.webServer> 
</configuration> 
+0

恩科西的web.config文件,爲感謝您的回答。 - 添加在根web.config「頁面」標籤 - 沒有變化。 - 添加您建議的web.config Views文件夾 - 新的錯誤如下: 編譯器錯誤消息:CS0234:類型或命名空間名稱「HTTP」在命名空間「System.Net」不存在(是否缺少程序組裝參考?) 源錯誤: 第26行:使用System.Web.Optimization; 27行:使用System.Web.Routing; 第28行:使用System.Net.Http; 第29行: 第30行: – user3104076

+0

@ user3104076,我說加上'pages'到'system.web'標籤,而不是根本。無論如何看起來你手上有更大的問題。嘗試從頭開始創建一個新項目,並將其配置與您的項目中不能正常工作的配置進行比較。應該給你一個關於什麼可能導致你的問題的線索。 – Nkosi