观看杨老师,杨旭Asp.Net Core MVC入门教程记录

ASP.NET Core MVC入门

  1. Asp.Net Core启动和配置
  • Program类,Main方法
  • Startup类
  1. 依赖注入,IoC容器
  • 生命周期
  • Transient:每次被请求都会创建新的实例
  • Scoped:每次Web请求会创建一个实例
  • Singleton:一旦被创建实例,就会一直使用这个实例,直到应用停止

依赖注入好处

  • 不用去管生命周期
  • 类型之间没有依赖

补充:

ILogger:在Microsoft.Extensions.Logging;命名空间

环境变量ASPNETCORE_ENVIRONMENT:

  • Development开发环境
  • Staging模拟环境
  • Production生产环境
  1. 前端工具
  • Npm:package.json
{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "bootstrap": "4.3.1",
    "jquery-slim": "3.0.0",
  }
}
  • Bundle和minify:bundleConfig.json
[
  {
    "outputFileName": "wwwroot/css/all.min.css",
    "inputFiles": [
      "node_modules/bootstrap/dist/css/bootstrap.css",
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/css/bootstrap.css",
    "inputFiles": [
      "node_modules/bootstrap/dist/css/bootstrap.css"
    ],
    "minify": {
      "enabled": false
    }
  }
]
  • BuildBundlerMinifier
  • Task Runners: Gulp,Grunt,Webpack
  1. 设置MVC
  • 注册MVC服务到IoC容器

  • 在ASP.NET Core管道里使用并配置MVC中间件

    在ConfigureServices方法中加入

    services.AddMvc();  
    

    在Configure方法中加入

     app.UseMvc(routes => 
              {
                  routes.MapRoute(
                      name: "default", 
                      template: "{controller=Home}/{action=Index}/{id?}");
              });
    
补充:app.UseStatusCodePages();显示错误码页

MVC  
+ Controller
+ Action
+ Filter
+ Model Binding
+ Routing
+ Attribute

## Views
+ Shared/_Layout.cshtml页面

## 配置
+ Key-Value
+ 内存,JSON,XML,INI,环境变量,启动参数
+ 从配置系统解耦
+ 为依赖注入优化

## View Component
+ 复用
+ Partial View无法包含业务逻辑
+ Controller里的逻辑无法复用

使用View Component有哪些优势?
+ 相当于PartialView带着一个小型的Controller
+ 可适用于教复杂业务
+ 可使用Razor语法

最后附上跟着教程写的代码部分
[ZZRRegionCoreDemo](https://github.com/ZZRRegion/CoreDemo)