日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC教程六:两个配置文件详解

發布時間:2024/7/5 asp.net 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC教程六:两个配置文件详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

在新建完一個MVC項目之后,你會發現整個整個項目結構中存在有兩個web.config文件,如下圖所示:

這兩個配置文件,一個位于項目的根目錄下面,一個位于Views文件夾下面,這兩個配置文件有什么不同呢?

一、根目錄下面的配置文件

跟目錄下面的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=\&quot;Web\&quot; /optionInfer+"/></compilers></system.codedom> </configuration>

?這個配置文件主要是用來配置數據庫連接字符串、日志輸出路徑等信息的,比如配置數據庫連接字符串

二、Views文件夾下面的配置文件

Views文件夾下面的配置文件主要是用來引入一些cshtml頁面中的命名空間

在上一篇文章中,我們如果要再cshtml視圖頁面中使用Student實體類,需要首先在頁面中引入Student的命名空間:

如果cshtml頁面都需要使用到Student類,那么每個頁面都需要先引入Student類的命名空間才可以使用,這樣會有很多重復的工作,可以把Student類的命名空間添加到Views文件夾下的配置文件中,這樣就不需要每個頁面都引入Student類的命名空間了

然后把ViewDataDemo對應的Index視圖修改如下:

@*引入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&nbsp;&nbsp;Name:@item.Name&nbsp;&nbsp;Age:@item.Age&nbsp;&nbsp;Sex:@item.Sex&nbsp;&nbsp;Email:@item.Email</div>}</div><div>5、傳遞集合方式二@foreach (var item in ViewData["Data"] as List<Student>){<div>ID:@item.ID&nbsp;&nbsp;Name:@item.Name&nbsp;&nbsp;Age:@item.Age&nbsp;&nbsp;Sex:@item.Sex&nbsp;&nbsp;Email:@item.Email</div>}</div></div> </div>

?注意:在Index視圖里面去掉命名空間以后,Student實體類會標紅,不影響程序。

重新生成程序,然后運行:

?

轉載于:https://www.cnblogs.com/dotnet261010/p/11417746.html

總結

以上是生活随笔為你收集整理的ASP.NET MVC教程六:两个配置文件详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。