一、.NET Core的项目结构
1.Properties:项目启动需要的一些配置:包含了端口号,IP地址
2.wwwroot:js/css/js包
3.MVC----模型试图控制
①.Controller:控制器—负责业务逻辑计算
②.表现层:用来展示各种结果,和用户互动
③.模型:串联在C–V之间,保存数据
4.appsettings:配置文件
5.Program控制台—ASP.NET Core5.0本质就是一个控制台
6.Startup:支持网站运行的一些相关配置
二、向页面传值
base代表父类(Controller)的别名
1.ViewBag
base.ViewBag.User1 = "张三";
2.ViewData
base.ViewData["User2"] = "李四";
3.TempData
base.TempData["User3"] = "王二麻子";
4.HttpContext.Session.SetString
使用Session需要配置
1.需要在视图中添加引用:@using Microsoft.AspNetCore.Http
2.Startup中ConfigureServices方法中增加:services.AddSession();
3.Startup中Configure方法中增加:app.UseSession();
base.HttpContext.Session.SetString("User4", "赵四儿");
配置展示:
Index.cshtml中:
Startup.cs中:
5.return View();
因为方法重载的原因,View()传入的值只能为object类型,传入string类型会自动被认为是传入的View名
object User5 = "刘六儿";
return View(User5);
页面上取值:
@using Microsoft.AspNetCore.Http
@{
<h3>User1=@base.ViewBag.User1 </h3>
<h3>User2=@base.ViewData["User2"]</h3>
<h3>User3=@base.TempData["User3"]</h3>
<h3>User4=@base.Context.Session.GetString("User4")</h3>
<h3>User5=@Model</h3>
}
运行出来的效果:
补充:
①.ViewBag 和 ViewData 内部实现是一样的,都是字典支持,如果key一样,后者的值会覆盖前者的值, 比如 :
base.ViewBag.User1 = "张三";
base.ViewData["User1"] = "张三~~张三";
在键同为User1的情况下,最后的运行结果为张三~~张三
②.跨页面传值可使用的传值方法有
TempData
本质是将值保存到了服务器内存中,但赋值后取值只能取一次,在一个控制器里赋值后,只能有一个视图中取值,在取了一次之后,后面再次取值则取不到
HttpContext.Session.SetString
本质是将值保存到了服务器内存中,赋值后,可随意取用
三、配置日志
关于NLog与Log4Net使用的帮助文档
>>>>>>>>>>>>>>>>>>>>>>>>>>>>NLog日志----文件记录: <<<<<<<<<<<<<<<<<<<<<<<<<<<
第一步:安装NuGet程序包
在解决方案上右击,选择管理解决方案的NuGet程序包
切换至浏览,在查找框中输入Nlog.Web.AspNetCore,选择包,选中所有项目,点击安装
搜索框中输入Nlog.Config,选中后选择所有项目,点击安装
安装成功后项目中会增加一个NLog.config
第二步:关于NLog.config
NLog.config文件默认是锁定不可修改状态,所以需要复制一份出去改完之后再覆盖回来
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--type:记录的文件类型 name:名称 fileName:全路径/名称 layout:记录文本-->
<target xsi:type="File" name="allfile" fileName = "Log\nlog-all-${shortdate}.log" layout="${longdata}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="File" name="ownFile-web" fileName = "Log\nlog-my-${shortdate}.log" layout="${longdata}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
</targets>
<!--用来选择记到哪里去-->
<rules>
<!-- add your logging rules here -->
<!--minlevel:日志最小级别 writeTo:要写入的日志name(上面targ