在新建完一個MVC項目以後,你會發現整個整個項目結構中存在有兩個web.config文件,以下圖所示:html
這兩個配置文件,一個位於項目的根目錄下面,一個位於Views文件夾下面,這兩個配置文件有什麼不一樣呢?web
跟目錄下面的web.config配置文件代碼以下:數據庫
<?xml version="1.0" encoding="utf-8"?> <!-- 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkId=301880 --> <configuration> <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> <compilation debug="true" targetFramework="4.6.1"/> <httpRuntime targetFramework="4.6.1"/> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f"/> <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"/> <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/> <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.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="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/> </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.WebPages" 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.4.0" newVersion="5.2.4.0"/> </dependentAssembly> </assemblyBinding> </runtime> <system.webServer> <modules> <remove name="TelemetryCorrelationHttpModule"/> <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler"/> </modules> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers> </system.codedom> </configuration>
這個配置文件主要是用來配置數據庫鏈接字符串、日誌輸出路徑等信息的,好比配置數據庫鏈接字符串c#
Views文件夾下面的配置文件主要是用來引入一些cshtml頁面中的命名空間app
在上一篇文章中,咱們若是要再cshtml視圖頁面中使用Student實體類,須要首先在頁面中引入Student的命名空間:dom
若是cshtml頁面都須要使用到Student類,那麼每一個頁面都須要先引入Student類的命名空間纔可使用,這樣會有不少重複的工做,能夠把Student類的命名空間添加到Views文件夾下的配置文件中,這樣就不須要每一個頁面都引入Student類的命名空間了ide
而後把ViewDataDemo對應的Index視圖修改以下:spa
@*引入Student的命名空間*@ @*@using MVCStudyDemo.Models; 去掉引入Student命名空間,在web.config文件裏面引入 *@ @{ ViewBag.Title = "Index"; // 這裏使用的是Razor語法,寫的是後臺C#代碼 // ViewData的Value值是Object類型的,須要進行類型轉換 // 常規寫法是先在這裏進行類型轉換 var list = ViewData["Data"] as List<Student>; } <h2>經過ViewData向View傳遞數據</h2> <div class="jumbotron"> <div> <div> 1、傳遞字符串 other:@ViewData["Other"]; </div> <div> 2、傳遞字符串 name:@ViewData["name"]; </div> <div> 3、傳遞字符串 age:@ViewData["age"]; </div> <div> 4、傳遞集合方式一 @foreach (var item in list) { <div> ID:@item.ID Name:@item.Name Age:@item.Age Sex:@item.Sex Email:@item.Email </div> } </div> <div> 5、傳遞集合方式二 @foreach (var item in ViewData["Data"] as List<Student>) { <div> ID:@item.ID Name:@item.Name Age:@item.Age Sex:@item.Sex Email:@item.Email </div> } </div> </div> </div>
注意:在Index視圖裏面去掉命名空間之後,Student實體類會標紅,不影響程序。debug
從新生成程序,而後運行:日誌