在新建完一个mvc项目之后,你会发现整个整个项目结构中存在有两个web.config文件,如下图所示:
这两个配置文件,一个位于项目的根目录下面,一个位于views文件夹下面,这两个配置文件有什么不同呢?
跟目录下面的web.config配置文件代码如下:
<?xml version="1.0" encoding="utf-8"?><!-- 有关如何配置 asp.net 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?linkid=301880 --><configuration> <appttings> <add key="webpages:version" value="3.0.0.0"/> <add key="webpages:enabled" value="fal"/> <add key="clientvalidationenabled" value="true"/> <add key="unobtrusivejavascriptenabled" value="true"/> </ap他生气了作文450字pttings> <system.web> <compilation debug="true" targetframework="4.6.1"/> <httpruntime targetframework="4.6.1"/> </system.web> <runtime> <asmblybinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentasmbly> <asmblyidentity name="antlr3.runtime" publickeytoken="eb42632606e9261f"/> <bindingredirect oldversion="0.0.0.0-3.5.0.2" newversion="3.5.0.2"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="system.diagnostics.diagnosticsox分之一的原函数urce" publickeytoken="cc7b13ffcd2ddd51"/> <bindingredirect oldversion="0.0.0.0-4.0.2.1" newversion="4.0.2.1"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="newtonsoft.json" publickeytoken="30ad4fe6b2a6aeed"/> <bindingredirect oldversion="0.0.0.0-11.0.0.0" newversion="11.0.0.0"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="system.web.optimization" publickeytoken="31bf3856ad364e35"/> <bindingredirect oldversion="1.0.0.0-1.1.0.0" newversion="1.1.0.0"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="webgrea" publickeytoken="31bf3856ad364e35"/> <bindingredirect oldversion="1.0.0.0-1.6.5135.21930" newversion="1.6.5135.21930"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="system.web.helpers" publickeytoken="31bf3856ad364e35"/> <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="system.web.webpages" publickeytoken="31bf3856ad364e35"/> <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0"/> </dependentasmbly> <dependentasmbly> <asmblyidentity name="system.web.mvc" publickeytoken="31bf3856ad364e35"/> <bindingredirect oldversion="1.0.0.0-5.2.4.0" newversion="5.2.4.0"/> </dependentasmbly> </asmblybinding> </runtime> <system.webrver> <modules> <remove name="telemetrycorrelationhttpmodule"/> <add name="telemetrycorrelationhttpmodule" type="microsoft.aspnet.telemetrycorrelation.telemetrycorrelationhttpmodule, microsoft.aspnet.telemetrycorrelation" precondition="integratedmode,managedhandler"/> </modules> </system.webrver> <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>
这个配置文件主要是用来配置数据库连接字符串、日志输出路径等信息的,比如配置数据库连接字符串
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["o朽木不折读音ther"]; </div> <div> 2、传递字符串 name:@viewdata["name"]; </div>内痔疮; <div> 3、传递字符串 age:@viewdata["age"]; </div> <div> 4、传递集合方式一 @foreach (var item in list) { <div> id:@item.idname:@item.nameage:@item.agex:@item.xemail:@item.email </div> } </div> <div> 5、传递集合方式二 @foreach (var item in viewdata["data"] as list<student>) { <div> id:@item.idname:@item.nameage:@item.agex:@item.xemail:@item.email </div> } </div> </div></div>
注意:在index视图里面去掉命名空间以后,student实体类会标红,不影响程序。
重新生成程序,然后运行:
到此这篇关于asp.net mvc中两个配置文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-06 04:42:40,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c00bea70cf64cc018ef94009f6f0ec5d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:ASP.NET MVC中两个配置文件的作用详解.doc
本文 PDF 下载地址:ASP.NET MVC中两个配置文件的作用详解.pdf
留言与评论(共有 0 条评论) |