# RankingWorld
**Repository Path**: loyalty-code/ranking-world
## Basic Information
- **Project Name**: RankingWorld
- **Description**: C#+ASP.NET Core + Dapper/EF Core
- **Primary Language**: C#
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2025-02-09
- **Last Updated**: 2025-09-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 介绍
ASP.NET Core 类似于Spring Boot,提供Web开发和依赖注入功能。
EF Core 和 Dapper 类似于 Mybatis,分别提供ORM和轻量级SQL映射功能
ASP.NET Core + EF Core 适合大多数应用场景,提供完整的ORM功能;
ASP.NET Core + Dapper 适合需要更高性能和灵活SQL控制的场景;
## 工程创建
```shell
Microsoft Windows [版本 10.0.19045.4046]
(c) Microsoft Corporation。保留所有权利。
D:\Desktop\projects-20240905\CSharp>dotnet new webapi -n RankingWorld
欢迎使用 .NET 5.0!
---------------------
SDK 版本: 5.0.103
遥测
---------
.NET 工具会收集用法数据,帮助我们改善你的体验。它由 Microsoft 收集并与社区共享。你可通过使用喜欢的 shell 将 DOTNET_CLI_TELEMETRY_OPTOUT 环境变量设置为 "1" 或 "true" 来选择退出遥测。
阅读有关 .NET CLI 工具遥测的更多信息: https://aka.ms/dotnet-cli-telemetry
----------------
已安装 ASP.NET Core HTTPS 开发证书。
若要信任该证书,请运行 "dotnet dev-certs https --trust" (仅限 Windows 和 macOS)。
了解 HTTPS: https://aka.ms/dotnet-https
----------------
编写你的第一个应用: https://aka.ms/dotnet-hello-world
查找新增功能: https://aka.ms/dotnet-whats-new
浏览文档: https://aka.ms/dotnet-docs
在 GitHub 上报告问题和查找源: https://github.com/dotnet/core
使用 "dotnet --help" 查看可用命令或访问: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Getting ready...
The template "ASP.NET Core Web API" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on RankingWorld\RankingWorld.csproj...
正在确定要还原的项目…
已还原 D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj (用时 6.49 sec)。
Restore succeeded.
D:\Desktop\projects-20240905\CSharp>cd RankingWorld
D:\Desktop\projects-20240905\CSharp\RankingWorld>dotnet add package Dapper
正在确定要还原的项目…
Writing D:\Temp\tmpA9BA.tmp
info : 正在将包“Dapper”的 PackageReference 添加到项目“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”。
info : GET https://api.nuget.org/v3/registration5-gz-semver2/dapper/index.json
info : OK https://api.nuget.org/v3/registration5-gz-semver2/dapper/index.json 796 毫秒
info : 正在还原 D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj 的包...
info : GET https://api.nuget.org/v3-flatcontainer/dapper/index.json
info : OK https://api.nuget.org/v3-flatcontainer/dapper/index.json 786 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/dapper/2.1.66/dapper.2.1.66.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/dapper/2.1.66/dapper.2.1.66.nupkg 147 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.reflection.emit.lightweight/index.json
info : OK https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/index.json 250 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/9.0.1/microsoft.bcl.asyncinterfaces.9.0.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/9.0.1/microsoft.bcl.asyncinterfaces.9.0.1.nupkg 103 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.reflection.emit.lightweight/index.json 556 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.reflection.emit.lightweight/4.7.0/system.reflection.emit.lightweight.4.7.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.reflection.emit.lightweight/4.7.0/system.reflection.emit.lightweight.4.7.0.nupkg 141 毫秒
info : 正在安装 System.Reflection.Emit.Lightweight 4.7.0。
info : 正在安装 Microsoft.Bcl.AsyncInterfaces 9.0.1。
info : 正在安装 Dapper 2.1.66。
info : 包“Dapper”与项目“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”中指定的所有框架均兼容 。
info : 包“Dapper”(版本为 2.1.66)的 PackageReference 已添加到文件“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”。
info : 正在提交还原...
info : 正在生成 MSBuild 文件 D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\RankingWorld.csproj.nuget.g.targets。
info : 将资产文件写入磁盘。路径: D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\project.assets.json
log : 已还原 D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj (用时 4.85 sec)。
D:\Desktop\projects-20240905\CSharp\RankingWorld>dotnet add package Mysql.Data
C:\Users\hasee\.nuget\packages\microsoft.bcl.asyncinterfaces\9.0.1\buildTransitive\netcoreapp2.0\Microsoft.Bcl.AsyncInterfaces.targets(4,5): warning : Microsoft.Bcl.AsyncInterfaces 9.0.1 doesn't support net5.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set true in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk. [D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj]
正在确定要还原的项目…
Writing D:\Temp\tmp289E.tmp
info : 正在将包“Mysql.Data”的 PackageReference 添加到项目“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”。
info : GET https://api.nuget.org/v3/registration5-gz-semver2/mysql.data/index.json
info : OK https://api.nuget.org/v3/registration5-gz-semver2/mysql.data/index.json 774 毫秒
info : 正在还原 D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj 的包...
info : GET https://api.nuget.org/v3-flatcontainer/mysql.data/index.json
info : OK https://api.nuget.org/v3-flatcontainer/mysql.data/index.json 2782 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/mysql.data/9.2.0/mysql.data.9.2.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/mysql.data/9.2.0/mysql.data.9.2.0.nupkg 208 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/bouncycastle.cryptography/index.json
info : GET https://api.nuget.org/v3-flatcontainer/google.protobuf/index.json
info : GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.buffers/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.configuration.configurationmanager/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.diagnostics.diagnosticsource/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.runtime.loader/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.security.permissions/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.text.encoding.codepages/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.text.json/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/index.json
info : GET https://api.nuget.org/v3-flatcontainer/zstdsharp.port/index.json
info : OK https://api.nuget.org/v3-flatcontainer/bouncycastle.cryptography/index.json 163 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/bouncycastle.cryptography/2.3.1/bouncycastle.cryptography.2.3.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/bouncycastle.cryptography/2.3.1/bouncycastle.cryptography.2.3.1.nupkg 152 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/index.json 651 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.configuration.configurationmanager/index.json 647 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/1.3.8/k4os.compression.lz4.streams.1.3.8.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.configuration.configurationmanager/8.0.0/system.configuration.configurationmanager.8.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.buffers/index.json 750 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/index.json 692 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/index.json 703 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.runtime.loader/index.json 701 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.buffers/4.5.1/system.buffers.4.5.1.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.5.4/system.threading.tasks.extensions.4.5.4.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/6.0.0/system.runtime.compilerservices.unsafe.6.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.runtime.loader/4.3.0/system.runtime.loader.4.3.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/1.3.8/k4os.compression.lz4.streams.1.3.8.nupkg 203 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.security.permissions/index.json 823 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.text.json/index.json 820 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.text.encoding.codepages/index.json 823 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/zstdsharp.port/index.json 818 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.security.permissions/8.0.0/system.security.permissions.8.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.text.json/9.0.0/system.text.json.9.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.text.encoding.codepages/8.0.0/system.text.encoding.codepages.8.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/zstdsharp.port/0.8.0/zstdsharp.port.0.8.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.configuration.configurationmanager/8.0.0/system.configuration.configurationmanager.8.0.0.nupkg 296 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.buffers/4.5.1/system.buffers.4.5.1.nupkg 221 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.diagnostics.diagnosticsource/index.json 1001 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.diagnostics.diagnosticsource/8.0.1/system.diagnostics.diagnosticsource.8.0.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.runtime.loader/4.3.0/system.runtime.loader.4.3.0.nupkg 371 毫 秒
info : OK https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.5.4/system.threading.tasks.extensions.4.5.4.nupkg 378 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/6.0.0/system.runtime.compilerservices.unsafe.6.0.0.nupkg 383 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.security.permissions/8.0.0/system.security.permissions.8.0.0.nupkg 284 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/index.json
info : GET https://api.nuget.org/v3-flatcontainer/k4os.hash.xxhash/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.io.pipelines/index.json
info : OK https://api.nuget.org/v3-flatcontainer/zstdsharp.port/0.8.0/zstdsharp.port.0.8.0.nupkg 528 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.diagnostics.diagnosticsource/8.0.1/system.diagnostics.diagnosticsource.8.0.1.nupkg 408 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.text.json/9.0.0/system.text.json.9.0.0.nupkg 564 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.text.encoding.codepages/8.0.0/system.text.encoding.codepages.8.0.0.nupkg 580 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.io.pipelines/index.json 504 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/index.json 510 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/k4os.hash.xxhash/index.json 508 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.io.pipelines/6.0.3/system.io.pipelines.6.0.3.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/k4os.hash.xxhash/1.0.8/k4os.hash.xxhash.1.0.8.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/1.3.8/k4os.compression.lz4.1.3.8.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.security.accesscontrol/index.json
info : OK https://api.nuget.org/v3-flatcontainer/k4os.hash.xxhash/1.0.8/k4os.hash.xxhash.1.0.8.nupkg 419 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/1.3.8/k4os.compression.lz4.1.3.8.nupkg 472 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.io.pipelines/6.0.3/system.io.pipelines.6.0.3.nupkg 478 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.security.accesscontrol/index.json 407 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.security.accesscontrol/6.0.0/system.security.accesscontrol.6.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.security.accesscontrol/6.0.0/system.security.accesscontrol.6.0.0.nupkg 463 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.memory/index.json
info : GET https://api.nuget.org/v3-flatcontainer/system.security.cryptography.protecteddata/index.json
info : OK https://api.nuget.org/v3-flatcontainer/system.memory/index.json 646 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/google.protobuf/index.json 3688 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.memory/4.5.5/system.memory.4.5.5.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/google.protobuf/3.26.1/google.protobuf.3.26.1.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.security.principal.windows/index.json
info : OK https://api.nuget.org/v3-flatcontainer/system.security.cryptography.protecteddata/index.json 521 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.security.cryptography.protecteddata/8.0.0/system.security.cryptography.protecteddata.8.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.memory/4.5.5/system.memory.4.5.5.nupkg 460 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/google.protobuf/3.26.1/google.protobuf.3.26.1.nupkg 465 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.security.principal.windows/index.json 548 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.security.principal.windows/5.0.0/system.security.principal.windows.5.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.security.cryptography.protecteddata/8.0.0/system.security.cryptography.protecteddata.8.0.0.nupkg 585 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.security.principal.windows/5.0.0/system.security.principal.windows.5.0.0.nupkg 456 毫秒
info : CACHE https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/index.json
info : GET https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/9.0.0/microsoft.bcl.asyncinterfaces.9.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.io.pipelines/9.0.0/system.io.pipelines.9.0.0.nupkg
info : GET https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/index.json
info : OK https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/9.0.0/microsoft.bcl.asyncinterfaces.9.0.0.nupkg 369 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.io.pipelines/9.0.0/system.io.pipelines.9.0.0.nupkg 456 毫秒
info : OK https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/index.json 471 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/9.0.0/system.text.encodings.web.9.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/9.0.0/system.text.encodings.web.9.0.0.nupkg 261 毫秒
info : 正在安装 System.Security.Principal.Windows 5.0.0。
info : 正在安装 System.Text.Encodings.Web 9.0.0。
info : 正在安装 System.IO.Pipelines 9.0.0。
info : 正在安装 Microsoft.Bcl.AsyncInterfaces 9.0.0。
info : 正在安装 System.Security.AccessControl 6.0.0。
info : 正在安装 System.Security.Cryptography.ProtectedData 8.0.0。
info : 正在安装 System.Memory 4.5.5。
info : 正在安装 K4os.Compression.LZ4 1.3.8。
info : 正在安装 System.IO.Pipelines 6.0.3。
info : 正在安装 K4os.Hash.xxHash 1.0.8。
info : 正在安装 System.Text.Json 9.0.0。
info : 正在安装 System.Text.Encoding.CodePages 8.0.0。
info : 正在安装 Google.Protobuf 3.26.1。
info : 正在安装 BouncyCastle.Cryptography 2.3.1。
info : 正在安装 System.Security.Permissions 8.0.0。
info : 正在安装 System.Configuration.ConfigurationManager 8.0.0。
info : 正在安装 MySql.Data 9.2.0。
info : 正在安装 System.Buffers 4.5.1。
info : 正在安装 System.Runtime.Loader 4.3.0。
info : 正在安装 System.Threading.Tasks.Extensions 4.5.4。
info : 正在安装 System.Runtime.CompilerServices.Unsafe 6.0.0。
info : 正在安装 K4os.Compression.LZ4.Streams 1.3.8。
info : 正在安装 System.Diagnostics.DiagnosticSource 8.0.1。
info : 正在安装 ZstdSharp.Port 0.8.0。
info : 包“Mysql.Data”与项目“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”中指定的所有框架均 兼容。
info : 包“Mysql.Data”(版本为 9.2.0)的 PackageReference 已添加到文件“D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj”。
info : 正在提交还原...
info : 正在生成 MSBuild 文件 D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\RankingWorld.csproj.nuget.g.targets。
info : 将资产文件写入磁盘。路径: D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\project.assets.json
log : 已还原 D:\Desktop\projects-20240905\CSharp\RankingWorld\RankingWorld.csproj (用时 20.22 sec)。
D:\Desktop\projects-20240905\CSharp\RankingWorld>
```
## 启动
```shell
# 重新生成项目
dotnet build
# 运行项目
dotnet run
```
## SQL Server
```shell
dotnet add package Microsoft.Data.sqlClient
```
## C# + ASP.NET Core + Dapper 开发的核心插件: VSCODE
```shell
C# + ASP.NET Core + Dapper 开发的核心插件列表:
C# (OmniSharp)
SQL Server (mssql) 或 SQLTools
ASP.NET Core Switcher
Razor 语言支持 -> Razor / Blazor CSS IntelliSense.
.NET Core Test Explorer
GitLens
REST Client
```
```shell
C# Dev Kit -> 安装这个,就包含上面的一些插件了
IntelliCode for C# Dev Kit 预览版 20250209
```
## 验证数据库链接
```c#
using MySql.Data.MySqlClient;
var connectionString = "Server=192.168.1.100;Port=3306;Database=RankingWorld;User Id=root;Password=password123;";
try
{
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("连接成功!");
}
}
catch (Exception ex)
{
Console.WriteLine("连接失败:" + ex.Message);
}
```
## 单元测试
```shell
# 创建解决方案文件(如果不存在)
dotnet new sln -n RankingWorld
# 创建一个新的单元测试项目
dotnet new xunit -n RankingWorld.Tests
# 将项目添加到解决方案
dotnet sln add RankingWorld.Tests/RankingWorld.Tests.csproj
# RankingWorld 项目还未添加到解决方案中,也需要添加:
dotnet sln add ./RankingWorld.csproj
# 验证解决方案文件
dotnet sln list
# 运行单元测试
dotnet test
# 测试项目中添加对 RankingWorld 项目的引用
cd RankingWorld.Tests
dotnet add reference ../RankingWorld.csproj
# 安装必要的 NuGet 包
# 确保测试项目中安装了以下 NuGet 包
dotnet add package xunit # xUnit 测试框架
dotnet add package Moq # 用于模拟依赖项(如果需要)
cd RankingWorld.Tests
dotnet test
cd RankingWorld.Tests
dotnet add package Microsoft.Extensions.Configuration
dotnet test
```
false
禁止自动生成:
D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\Debug\net5.0\RankingWorld.AssemblyInfo.cs
```c#
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("RankingWorld")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("RankingWorld")]
[assembly: System.Reflection.AssemblyTitleAttribute("RankingWorld")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// 由 MSBuild WriteCodeFragment 类生成。
```
false 对这个文件不起作用,还是会自动生成:
D:\Desktop\projects-20240905\CSharp\RankingWorld\obj\Debug\net5.0\.NETCoreApp,Version=v5.0.AssemblyAttributes.cs
```c#
//
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")]
```
```c#
// 测试目录,加:false
// 删除以上两个文件
RankingWorld\RankingWorld.Tests>
dotnet build
dotnet test
```
## 覆盖率
```c#
cd RankingWorld.Tests
// 使用工具(如 Coverlet)来检查测试覆盖率
dotnet add package coverlet.msbuild
# 像上面一样,删除文件再运行,否则报错
# 运行测试并生成覆盖率报告
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
# 单元测试中有失败的测试用例,dotnet test 命令可能会提前终止,导致覆盖率报告未能生成。覆盖率报告通常只在所有测试通过时才会生成
# 强制生成覆盖率报告: 使用 --no-build 参数
dotnet test --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
# 使用 --logger 参数
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger "trx;LogFileName=testresults.trx"
# 手动指定输出目录
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./TestResults/
# 查看覆盖率报告 -> 报告目录都没有生成,报错???
# 安装 ReportGenerator
dotnet tool install -g dotnet-reportgenerator-globaltool
# error NU1202: 包 dotnet-reportgenerator-globaltool 5.4.3 与 net5.0 (.NETCoreApp,Version=v5.0) / any 不兼容。 包 dotnet-reportgenerator-globaltool 5.4.3 支持:
# 生成 HTML 报告
reportgenerator -reports:TestResults/coverage.opencover.xml -targetdir:coveragereport
```
## 启动2
```shell
# 清理 NuGet 缓存
dotnet nuget locals all --clear
dotnet run
```
http://localhost:5000/WeatherForecast/forecast
```shell
# [Route("[controller]")]
WeatherForecastController 使用了 [Route("[controller]")] 属性,这意味着控制器的基础路由是控制器名称(去掉 Controller 后缀)。因此,WeatherForecastController 的基础路由是 /WeatherForecast
```
[Route("api/weather")] // 自定义路由
[HttpGet("forecast")] // 自定义路径