3小时搞定asp.net 9.0

MVC基础篇
  • IDE: Visual Studio 2022 -community
  • DB:MySQL5.7+

下载V9.0:

建立工程

File-》New-》Project

ASP.NET Core Web Application(Model-View-Controller)

vs2022 preview

简单修改主页

Solution Explorer

Views-》Home-》Index.cshtml

增加如下代码:

<p>这是我的第1个ASP.NET程序!</p>

选择使用何种Web Server 运行:

  • http
  • https
  • IIS Express

default: https

点击“IIS Express”运行(如果弹出证书安装选“是”即可)。 iis cert - hint windows - hint

增加数据库

EF Core

Project-》Manage NuGet Packages Microsoft EF Core

entity class

我们建立一个与数据表相对应的Entity类

using System.ComponentModel.DataAnnotations;

namespace veic_web.Models
{
    public class Product
    {
        [Key]
        public int Id { get; set; }
        public int Param_id { get; set; }
        public int Statu_id { get; set; }
        public int Lang_id { get; set; }
        public int Img_id { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
    }
}

DB Class

Solution Explorer

Model右键点击Add-》New Item选择Class Name: ApplicationDbContext.cs

添加引用:

using Microsoft.EntityFrameworkCore;

添加继承:DbContext;
and “Generate Constructor ‘ApplicationDbContext(options)’"(shortcut: Ctrl+. or Alt+Enter)

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
}

代码模板在DbContext.cs 添加Entity类支持

public DbSet<Product> Products
{
    get; set;
}

Repository操作类

接口:

Solution Explorer -》 Model右键点击Add-》New Item选择Interface

Name: IProductRepository.cs

public interface IProductRepository
{
    IQueryable<Product> Products { get; }
}

实现:

Solution Explorer -》 Model右键点击Add-》New Item选择Class

Name: EFProductRepository.cs

  • Use primary Constructor
public class EFProductRepository(ApplicationDbContext context) : IProductRepository
{
    public IQueryable<Product> Products => context.Products;
}

connector

The screenshots use MySQL EF Core v8.0.5; It does not work on .net9.0!
Version 9.0.0 released in January 2025 supports.

添加MySQLConnector

Project-》Manager NuGet Packages MySql EF Core

connection string

  • appsettings.json
"Data": {
    "VeicWeb": {
        "ConnectionString": "server=127.0.0.1; user id=DBAdmin; password=xbfirst; database=carnumber; pooling=false; Convert Zero Datetime=True;"
    }
},

前后代码比较: ultra compare p0

配置DI(dependency injection)

Program.cs

Configure Services

添加数据库context

//// MySql - Oracle
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySQL(builder.Configuration["Data:VeicWeb:ConnectionString"]));

添加Repository

builder.Services.AddTransient<IProductRepository, EFProductRepository>();

前后代码比较: ultra compare p1

添加View

Index.cshtml

@model IEnumerable<Product>
@foreach (var p in Model)
{
    <div>
        <h3>@p.Name</h3>
        @p.Description
    </div>
}

前后代码比较: ultra compare p2

添加Controller

HomeController.cs

引用实例:

private readonly IProductRepository _productRepository;

public HomeController(ILogger<HomeController> logger, IProductRepository productRepository)
{
    _logger = logger;
    _productRepository = productRepository;
}

调用他:

public IActionResult Index()
{
    return View(_productRepository.Products);
}

前后代码比较: ultra compare p3

运行效果: 演示code

Reference