添加CSharp

This commit is contained in:
bunny 2025-06-22 20:21:42 +08:00
parent b73a52c022
commit a060613e96
465 changed files with 9545 additions and 0 deletions

20
.gitignore vendored
View File

@ -62,3 +62,23 @@ coverage
*.sw? *.sw?
*.tsbuildinfo *.tsbuildinfo
logs/
[b|B]in
[o|O]bj
.vs
[E|e]xclude
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/.idea.SQLTutorial.iml
/modules.xml
/projectSettingsUpdater.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ADO_1_Start</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ADO.Net.Client" Version="1.4.4" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,36 @@
using MySqlConnector;
namespace ADO_1_Start;
public class Program
{
private static readonly string Host = "rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com";
private static readonly string Database = "bunny_test";
private static readonly uint Port = 3306;
private static readonly string Username = "root";
private static readonly string Password = "0212zkw!";
public static void Main(string[] args)
{
var mySqlConnectionStringBuilder = new MySqlConnectionStringBuilder
{
Server = Host, Port = Port,
Database = Database,
UserID = Username, Password = Password
};
Console.WriteLine(mySqlConnectionStringBuilder.ConnectionString);
using (var mySqlConnection = new MySqlConnection(mySqlConnectionStringBuilder.ConnectionString))
{
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand("select * from sys_user");
mySqlCommand.Connection = mySqlConnection;
var mySqlDataReader = mySqlCommand.ExecuteReader();
mySqlDataReader.Read();
for (var i = 0; i < mySqlDataReader.FieldCount; i++)
Console.Write(mySqlDataReader.GetName(i) + (i < mySqlDataReader.FieldCount - 1 ? "\t" : "\n"));
}
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>WebApplication1</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.16"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-preview.5.25277.114" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Data;
namespace WebApplication1.Controller;
[Route("api/[controller]")]
public class HomeController
{
[HttpGet]
public string? Index()
{
var configuration = AppCConfigurationServices.Configuration;
return configuration.GetConnectionString("BunnyTest");
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.Extensions.Configuration.Json;
namespace WebApplication1.Data;
public class AppCConfigurationServices
{
static AppCConfigurationServices()
{
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource
{
Path = "appsettings.json",
ReloadOnChange = true
}).Build();
}
public static IConfiguration Configuration { get; set; }
}

View File

@ -0,0 +1,27 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder => policyBuilder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:13883",
"sslPort": 44388
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5153",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7156;http://localhost:5153",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,6 @@
@WebApplication1_HostAddress = http://localhost:5153
GET {{WebApplication1_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BunnyTest": "Server=rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com;Port=3306;User ID=root;Password=0212zkw!;Database=bunny_test"
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ADO_Web_2_Connect</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.16"/>
<PackageReference Include="MySqlConnector" Version="2.4.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,76 @@
using ADO_Web_2_Connect.Data;
using ADO_Web_2_Connect.Helper;
using ADO_Web_2_Connect.Model;
using Microsoft.AspNetCore.Mvc;
namespace ADO_Web_2_Connect.Controller;
[Route("/api/[controller]")]
public class HomeController : ControllerBase
{
[HttpGet("Index")]
public IActionResult Index()
{
var connectionString = AppConfigurationServices.Configuration.GetConnectionString("BunnyTest");
return new OkObjectResult(connectionString);
}
[HttpGet]
public async Task<ActionResult<List<NewType>>> GetAll()
{
const string sql = "SELECT * FROM news_type";
var readerAsync = await MysqlHelper.ExecuteReaderAsync(sql);
var newTypes = new List<NewType>();
if (!readerAsync.HasRows) return newTypes;
while (readerAsync.Read())
newTypes.Add(new NewType
{
Id = readerAsync.GetInt32(0), NewsTypeTitle = readerAsync.GetString(1),
IsEnabled = readerAsync.GetBoolean(2)
});
readerAsync.Close();
return newTypes;
}
[HttpGet("GetById/{id:int}")]
public async Task<ActionResult<NewType>> GetById(int id)
{
var sql = $"SELECT * FROM news_type WHERE id={id}";
var mySqlDataReader = await MysqlHelper.ExecuteReaderAsync(sql);
// 是否存在数据
if (!mySqlDataReader.HasRows) return new NotFoundResult();
var newType = new NewType();
while (mySqlDataReader.Read())
newType = new NewType
{
Id = mySqlDataReader.GetInt32(0),
NewsTypeTitle = mySqlDataReader.GetString(1),
IsEnabled = mySqlDataReader.GetBoolean(2)
};
mySqlDataReader.Close();
return newType;
}
[HttpPost]
public async Task<int> Insert(NewType? newType)
{
if (newType == null) return 0;
var sql =
$"INSERT INTO news_type ( `id`, `news_type_title`, `is_enable` ) VALUES ( {newType.Id}, '{newType.NewsTypeTitle}', {newType.IsEnabled} )";
return await MysqlHelper.ExecuteQueryAsync(sql);
}
[HttpDelete]
public async Task<int> DeleteById(int id)
{
var sql = $"DELETE FROM news_type WHERE id={id}";
return await MysqlHelper.ExecuteQueryAsync(sql);
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.Extensions.Configuration.Json;
namespace ADO_Web_2_Connect.Data;
public class AppConfigurationServices
{
static AppConfigurationServices()
{
Configuration = new ConfigurationBuilder().Add(new JsonConfigurationSource
{
Path = "appsettings.json",
ReloadOnChange = true
}).Build();
}
public static IConfiguration Configuration { get; set; }
}

View File

@ -0,0 +1,108 @@
using System.Data;
using ADO_Web_2_Connect.Data;
using MySqlConnector;
namespace ADO_Web_2_Connect.Helper;
public class MysqlHelper
{
private static readonly string? ConnStr;
static MysqlHelper()
{
ConnStr ??= AppConfigurationServices.Configuration.GetConnectionString("BunnyTest");
}
/// <summary>
/// 查询数据库
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql)
{
using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var command = new MySqlCommand(sql, mySqlConnection);
return command.ExecuteNonQuery();
}
/// <summary>
/// 异步查询数据库
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<int> ExecuteQueryAsync(string sql)
{
await using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var command = new MySqlCommand(sql, mySqlConnection);
return await command.ExecuteNonQueryAsync();
}
/// <summary>
/// 读取数据库流
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static MySqlDataReader ExecuteReader(string sql)
{
using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var command = new MySqlCommand(sql, mySqlConnection);
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 异步读取数据库流
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<MySqlDataReader> ExecuteReaderAsync(string sql)
{
var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var command = new MySqlCommand(sql, mySqlConnection);
return await command.ExecuteReaderAsync();
}
/// <summary>
/// 异步读取数据库流
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<MySqlDataReader> ExecuteReaderAsyncTest(string sql)
{
await using var mySqlConnection = new MySqlConnection();
mySqlConnection.Open();
var command = new MySqlCommand(sql, mySqlConnection);
return await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
}
/// <summary>
/// 获取首行首列的值
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static object? ExecuteScalar(string sql)
{
using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = sql;
mySqlCommand.CommandType = CommandType.Text;
return mySqlCommand.ExecuteScalar();
}
/// <summary>
/// 异步获取首行首列
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<object?> ExecuteScalarAsync(string sql)
{
await using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return await mySqlCommand.ExecuteScalarAsync();
}
}

View File

@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations.Schema;
namespace ADO_Web_2_Connect.Model;
public class NewType
{
/// <summary>
/// 新闻分类Id
/// </summary>
[Column("id")]
public int Id { get; set; }
/// <summary>
/// 新闻分类标题
/// </summary>
[Column("news_type_title")]
public string? NewsTypeTitle { get; set; }
/// <summary>
/// 是否启用
/// </summary>
[Column("is_enable")]
public bool? IsEnabled { get; set; }
}

View File

@ -0,0 +1,28 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder => policyBuilder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
});
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:17876",
"sslPort": 44385
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5042",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7259;http://localhost:5042",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BunnyTest": "Server=rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com;Port=3306;User ID=root;Password=0212zkw!;Database=news"
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ADO_Web_3_Connect</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.16"/>
<PackageReference Include="MySqlConnector" Version="2.4.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,91 @@
using ADO_Web_3_Connect.Helper;
using ADO_Web_3_Connect.Model;
using Microsoft.AspNetCore.Mvc;
namespace ADO_Web_3_Connect.Controller;
[Route("api/[controller]")]
public class NewsController : ControllerBase
{
[HttpGet]
public async Task<List<News>> GetNews()
{
const string sql = "select * from news";
var mySqlDataReader = await MysqlHelper.ExecuteReaderAsync(sql);
var news = new List<News>();
while (mySqlDataReader.Read())
news.Add(new News
{
Id = mySqlDataReader.GetInt32(0),
Title = mySqlDataReader.GetString(1),
Content = mySqlDataReader.GetString(2),
NewsTypeId = mySqlDataReader.GetInt32(3)
});
return news;
}
[HttpGet("{id:int}")]
public async Task<News> GetNewsById(int id)
{
var sql =
$"SELECT n.id, n.news_title, n.content, n.new_type AS news_type_id, nt.news_type_title FROM news n LEFT JOIN news_type nt ON nt.id = n.new_type WHERE n.id = {id}";
var readerAsync = await MysqlHelper.ExecuteReaderAsync(sql);
var news = new News();
while (readerAsync.Read())
news = new News
{
Id = readerAsync.GetInt32(0),
Title = readerAsync.GetString(1),
Content = readerAsync.GetString(2),
NewsTypeId = readerAsync.GetInt32(3),
NewsTypeTitle = readerAsync.GetString(4)
};
readerAsync.Close();
return news;
}
[HttpGet("Query")]
public async Task<List<News>> Query(string keyword)
{
var sql =
$"SELECT n.id, n.news_title, n.content, n.new_type AS news_type_id, nt.news_type_title FROM news n LEFT JOIN news_type nt ON nt.id = n.new_type WHERE n.content LIKE '%{keyword}%' OR n.news_title LIKE '%{keyword}%'";
var mySqlDataReader = await MysqlHelper.ExecuteReaderAsync(sql);
var list = new List<News>();
while (mySqlDataReader.Read())
{
var news = new News
{
Id = mySqlDataReader.GetInt32(0),
Title = mySqlDataReader.GetString(1),
Content = mySqlDataReader.GetString(2),
NewsTypeId = mySqlDataReader.GetInt32(3),
NewsTypeTitle = mySqlDataReader.GetString(4)
};
list.Add(news);
}
mySqlDataReader.Close();
return list;
}
[HttpPost]
public async Task<int> Create(News? news)
{
if (news == null) return 0;
var sql =
$"insert into news (id, news_title, content, new_type) values ({news.Id},'{news.Title}','{news.Content}',{news.NewsTypeId})";
return await MysqlHelper.ExecuteNoeQueryAsync(sql);
}
[HttpDelete("{id:int}")]
public async Task<ActionResult<int>> Delete(int id)
{
var executeNoeQueryAsync = await MysqlHelper.ExecuteNoeQueryAsync($"delete from news where id={id}");
return executeNoeQueryAsync;
}
}

View File

@ -0,0 +1,76 @@
using ADO_Web_3_Connect.Data;
using ADO_Web_3_Connect.Helper;
using ADO_Web_3_Connect.Model;
using Microsoft.AspNetCore.Mvc;
namespace ADO_Web_3_Connect.Controller;
[Route("/api/[controller]")]
public class NewsTypeController : ControllerBase
{
[HttpGet("Index")]
public IActionResult Index()
{
var connectionString = AppConfigurationServices.Configuration.GetConnectionString("BunnyTest");
return new OkObjectResult(connectionString);
}
[HttpGet]
public async Task<ActionResult<List<NewType>>> GetAll()
{
const string sql = "SELECT * FROM news_type";
var readerAsync = await MysqlHelper.ExecuteReaderAsync(sql);
var newTypes = new List<NewType>();
if (!readerAsync.HasRows) return newTypes;
while (readerAsync.Read())
newTypes.Add(new NewType
{
Id = readerAsync.GetInt32(0), NewsTypeTitle = readerAsync.GetString(1),
IsEnabled = readerAsync.GetBoolean(2)
});
readerAsync.Close();
return newTypes;
}
[HttpGet("GetById/{id:int}")]
public async Task<ActionResult<NewType>> GetById(int id)
{
var sql = $"SELECT * FROM news_type WHERE id={id}";
var mySqlDataReader = await MysqlHelper.ExecuteReaderAsync(sql);
// 是否存在数据
if (!mySqlDataReader.HasRows) return new NotFoundResult();
var newType = new NewType();
while (mySqlDataReader.Read())
newType = new NewType
{
Id = mySqlDataReader.GetInt32(0),
NewsTypeTitle = mySqlDataReader.GetString(1),
IsEnabled = mySqlDataReader.GetBoolean(2)
};
mySqlDataReader.Close();
return newType;
}
[HttpPost]
public async Task<int> Insert(NewType? newType)
{
if (newType == null) return 0;
var sql =
$"INSERT INTO news_type ( `id`, `news_type_title`, `is_enable` ) VALUES ( {newType.Id}, '{newType.NewsTypeTitle}', {newType.IsEnabled} )";
return await MysqlHelper.ExecuteNoeQueryAsync(sql);
}
[HttpDelete]
public async Task<int> DeleteById(int id)
{
var sql = $"DELETE FROM news_type WHERE id={id}";
return await MysqlHelper.ExecuteNoeQueryAsync(sql);
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.Extensions.Configuration.Json;
namespace ADO_Web_3_Connect.Data;
public class AppConfigurationServices
{
public static IConfiguration Configuration;
static AppConfigurationServices()
{
Configuration = new ConfigurationBuilder().Add(new JsonConfigurationSource
{
Path = "appsettings.json",
ReloadOnChange = true
}).Build();
}
}

View File

@ -0,0 +1,93 @@
using System.Data;
using ADO_Web_3_Connect.Data;
using MySqlConnector;
namespace ADO_Web_3_Connect.Helper;
public class MysqlHelper
{
private static readonly string? ConnStr;
static MysqlHelper()
{
ConnStr ??= AppConfigurationServices.Configuration.GetConnectionString("BunnyTest");
}
/// <summary>
/// 查询数据库
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql)
{
using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return mySqlCommand.ExecuteNonQuery();
}
/// <summary>
/// 异步查询数据库
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<int> ExecuteNoeQueryAsync(string sql)
{
var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return await mySqlCommand.ExecuteNonQueryAsync();
}
/// <summary>
/// 读取数据库流
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static MySqlDataReader ExecuteReader(string sql)
{
var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 异步读取数据库流
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<MySqlDataReader> ExecuteReaderAsync(string sql)
{
var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return await mySqlCommand.ExecuteReaderAsync();
}
/// <summary>
/// 获取首行首列的值
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static object? ExecuteScalar(string sql)
{
using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return mySqlCommand.ExecuteScalar();
}
/// <summary>
/// 异步获取首行首列
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static async Task<object?> ExecuteScalarAsync(string sql)
{
await using var mySqlConnection = new MySqlConnection(ConnStr);
mySqlConnection.Open();
var mySqlCommand = new MySqlCommand(sql, mySqlConnection);
return await mySqlCommand.ExecuteScalarAsync();
}
}

View File

@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations.Schema;
namespace ADO_Web_3_Connect.Model;
public class NewType
{
/// <summary>
/// 新闻分类Id
/// </summary>
[Column("id")]
public int Id { get; set; }
/// <summary>
/// 新闻分类标题
/// </summary>
[Column("news_type_title")]
public string? NewsTypeTitle { get; set; }
/// <summary>
/// 是否启用
/// </summary>
[Column("is_enable")]
public bool? IsEnabled { get; set; }
}

View File

@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations.Schema;
namespace ADO_Web_3_Connect.Model;
public class News
{
/// <summary>
/// 主键
/// </summary>
[Column("id")]
public int Id { get; set; }
/// <summary>
/// 新闻标题
/// </summary>
[Column("news_title")]
public string? Title { get; set; }
/// <summary>
/// 新闻内容
/// </summary>
[Column("news_content")]
public string? Content { get; set; }
/// <summary>
/// 新闻类型id
/// </summary>
[Column("news_type")]
public int? NewsTypeId { get; set; }
[NotMapped] public string? NewsTypeTitle { get; set; }
}

View File

@ -0,0 +1,30 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
});
});
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50314",
"sslPort": 44336
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5071",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7259;http://localhost:5071",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BunnyTest": "Server=rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com;Port=3306;User ID=root;Password=0212zkw!;Database=news"
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Cors_1_Start</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.16"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,33 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy("Cors示例",
policyBuilder => { policyBuilder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); });
});
builder.Services.AddControllers();
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("Cors示例");
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:34628",
"sslPort": 44317
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5256",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7252;http://localhost:5256",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,28 @@
using EFCore_1_Start.Model;
using Microsoft.EntityFrameworkCore;
namespace EFCore_1_Start.Context;
/// <summary>
/// EFCore上下文对象
/// </summary>
public class EfCoreContext : DbContext
{
/// <summary>
/// EFCore上下文对象
/// </summary>
/// <param name="options">DbContextOptions</param>
public EfCoreContext(DbContextOptions options) : base(options)
{
}
/// <summary>
/// 产品分类信息
/// </summary>
public DbSet<PCategory> PCategories { get; set; }
/// <summary>
/// 产品信息
/// </summary>
public DbSet<Product> Products { get; set; }
}

View File

@ -0,0 +1,72 @@
using EFCore_1_Start.Model;
using EFCore_1_Start.Service.IServices;
using Microsoft.AspNetCore.Mvc;
namespace EFCore_1_Start.Controller;
/// <summary>
/// PCategory控制器
/// </summary>
[Route("api/[controller]")]
public class PCategoryController : ControllerBase
{
private readonly IPCategoryService _ipCategoryService;
/// <summary>
/// PCategory服务
/// </summary>
/// <param name="ipCategoryService">IPCategoryService</param>
public PCategoryController(IPCategoryService ipCategoryService)
{
_ipCategoryService = ipCategoryService;
}
/// <summary>
/// 获取所有产品信息
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult<List<PCategory>> GetAll()
{
return _ipCategoryService.GetAll();
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id:guid}")]
public ActionResult<PCategory?> GetById(Guid id)
{
var pCategory = _ipCategoryService.GetById(id);
return pCategory;
}
/// <summary>
/// 创建产品分类
/// </summary>
/// <param name="pCategory"></param>
/// <returns></returns>
[HttpPost]
public async Task<int> CreatePCategory(PCategory? pCategory)
{
if (pCategory == null) return 0;
pCategory.CId = Guid.NewGuid();
return await _ipCategoryService.CreatePCategory(pCategory);
}
/// <summary>
/// 根据Id删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpDelete("{id:guid}")]
public async Task<int> DeletePCategoryById(Guid? id)
{
if (id == null) return 0;
return await _ipCategoryService.DeletePCategoryById(id);
}
}

View File

@ -0,0 +1,81 @@
using EFCore_1_Start.Model;
using EFCore_1_Start.Service.IServices;
using Microsoft.AspNetCore.Mvc;
namespace EFCore_1_Start.Controller;
/// <summary>
/// 产品控制器
/// </summary>
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
/// <summary>
/// ProductController
/// </summary>
/// <param name="productService">IProductService</param>
public ProductController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>ActionResult - Product</returns>
[HttpGet("QueryListById/{id:guid}")]
public async Task<ActionResult<List<Product>>> QueryListById(Guid id)
{
return await _productService.QueryListById(id);
}
/// <summary>
/// 根据分类Id查询产品信息
/// </summary>
/// <param name="cId">分类Id</param>
/// <param name="keyword"></param>
/// <returns>Product</returns>
[HttpGet("QueryByKeyword")]
public async Task<ActionResult<List<Product>>> QueryByKeyword(Guid cId, string keyword)
{
return await _productService.QueryByKeyword(cId, keyword);
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Product</returns>
[HttpGet("{id:guid}")]
public async Task<ActionResult<Product?>> GetById(Guid id)
{
return await _productService.GetById(id);
}
/// <summary>
/// 创建产品
/// </summary>
/// <param name="product">产品</param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<int>> CreateProduct(Product? product)
{
if (product == null) return 0;
return await _productService.CreateProduct(product);
}
/// <summary>
/// 根据Id删除产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Task</returns>
[HttpDelete("{id:guid}")]
public async Task<int> DeleteProduct(Guid id)
{
return await _productService.DeleteProduct(id);
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>EFCore_1_Start</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="9.0.0-preview.3.efcore.9.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCore_1_Start.Model;
/// <summary>
/// 产品分类
/// </summary>
[Table("p_category")]
public class PCategory
{
/// <summary>
/// 主键
/// </summary>
[Key]
public Guid? CId { get; set; } = Guid.NewGuid();
/// <summary>
/// 分类标题
/// </summary>
[MaxLength(100)]
public string? CTitle { get; set; }
}

View File

@ -0,0 +1,45 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCore_1_Start.Model;
/// <summary>
/// 产品
/// </summary>
[Table("product")]
public class Product
{
/// <summary>
/// 产品Id
/// </summary>
[Key]
public Guid? PId { get; set; } = Guid.NewGuid();
/// <summary>
/// 产品名称
/// </summary>
[MaxLength(200)]
public string? PTitle { get; set; }
/// <summary>
/// 产品数量
/// </summary>
public int PSum { get; set; }
/// <summary>
/// 产品价格
/// </summary>
[Column(TypeName = "decimal(18,2)")]
public decimal PPrice { get; set; }
/// <summary>
/// 产品分类Id
/// </summary>
public Guid PCategoryId { get; set; }
/// <summary>
/// 产品分类信息
/// </summary>
[ForeignKey("PCategoryId")]
public virtual PCategory? PCategory { get; set; }
}

View File

@ -0,0 +1,78 @@
using System.Diagnostics;
using System.Reflection;
using EFCore_1_Start.Context;
using EFCore_1_Start.Repositories;
using EFCore_1_Start.Repositories.IRepository;
using EFCore_1_Start.Service;
using EFCore_1_Start.Service.IServices;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
});
});
// 注册 DbContext 服务
builder.Services.AddDbContext<EfCoreContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("BunnyTest") ?? string.Empty;
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});
// 添加控制器
builder.Services.AddControllers();
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
// ⭐ 添加 XML 注释支持
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// 注入依赖
builder.Services.AddTransient<IProductService, ProductService>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddTransient<IPCategoryService, PCategoryService>();
builder.Services.AddScoped<IPCategoryRepository, PCategoryRepository>();
var app = builder.Build();
// 初始化数据库Code First
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<EfCoreContext>();
// 1. ⚠️ 注意EnsureDeleted() 会 永久删除整个数据库,仅适用于开发和测试环境!
// var ensureDeleted = dbContext.Database.EnsureDeleted();
// Console.WriteLine(ensureDeleted ? "Database created successfully!" : "Database already exists.");
// 2. 如果数据库不存在,则创建(仅用于开发环境)
var dbCreated = dbContext.Database.EnsureCreatedAsync();
Debug.WriteLine(await dbCreated ? "Database created successfully!" : "Database already exists.");
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.MapSwagger();
app.UseSwaggerUI();
}
// app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:32321",
"sslPort": 44378
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5125",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7224;http://localhost:5125",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,36 @@
using EFCore_1_Start.Model;
namespace EFCore_1_Start.Repositories.IRepository;
/// <summary>
/// 产品分类数据库
/// </summary>
public interface IPCategoryRepository
{
/// <summary>
/// 创建产品分类
/// </summary>
/// <param name="pCategory"></param>
/// <returns></returns>
Task<int> CreatePCategory(PCategory? pCategory);
/// <summary>
/// 获取所有产品信息
/// </summary>
/// <returns></returns>
List<PCategory> GetAll();
/// <summary>
/// 根据id查找分类
/// </summary>
/// <param name="id">Id</param>
/// <returns></returns>
PCategory? GetById(Guid id);
/// <summary>
/// 根据Id删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
Task<int> DeletePCategoryById(Guid? id);
}

View File

@ -0,0 +1,45 @@
using EFCore_1_Start.Model;
namespace EFCore_1_Start.Repositories.IRepository;
/// <summary>
/// 产品数据库查询
/// </summary>
public interface IProductRepository
{
/// <summary>
/// 创建产品
/// </summary>
/// <param name="product">产品表单</param>
/// <returns>Task</returns>
Task<int> Create(Product product);
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>ActionResult - Product</returns>
Task<List<Product>> QueryListById(Guid id);
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Product</returns>
Task<Product?> GetById(Guid id);
/// <summary>
/// 根据Id删除产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Task</returns>
Task<int> DeleteProduct(Guid id);
/// <summary>
/// 根据分类Id查询产品信息
/// </summary>
/// <param name="cId">分类Id</param>
/// <param name="keyword"></param>
/// <returns>Product</returns>
Task<List<Product>> QueryByKeyword(Guid cId, string keyword);
}

View File

@ -0,0 +1,56 @@
using EFCore_1_Start.Context;
using EFCore_1_Start.Model;
using EFCore_1_Start.Repositories.IRepository;
namespace EFCore_1_Start.Repositories;
/// <summary>
/// 产品分类数据库查询实现
/// </summary>
/// <param name="context">EfCoreContext</param>
public class PCategoryRepository(EfCoreContext context) : IPCategoryRepository
{
/// <summary>
/// 创建产品分类
/// </summary>
/// <param name="pCategory"></param>
/// <returns></returns>
public Task<int> CreatePCategory(PCategory? pCategory)
{
if (pCategory == null) return Task.FromResult(0);
context.PCategories.Add(pCategory);
var saveChangesAsync = context.SaveChangesAsync();
return saveChangesAsync;
}
/// <summary>
/// 获取所有产品信息
/// </summary>
/// <returns></returns>
public List<PCategory> GetAll()
{
return context.PCategories.ToList();
}
/// <summary>
/// 根据id查找分类
/// </summary>
/// <param name="id">Id</param>
/// <returns></returns>
public PCategory? GetById(Guid id)
{
return context.PCategories.Find(id);
}
/// <summary>
/// 根据Id删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
public Task<int> DeletePCategoryById(Guid? id)
{
var pCategory = new PCategory { CId = id };
context.PCategories.Remove(pCategory);
return context.SaveChangesAsync();
}
}

View File

@ -0,0 +1,78 @@
using EFCore_1_Start.Context;
using EFCore_1_Start.Model;
using EFCore_1_Start.Repositories.IRepository;
using Microsoft.EntityFrameworkCore;
namespace EFCore_1_Start.Repositories;
/// <summary>
/// Product数据库查询
/// </summary>
/// <param name="context"></param>
public class ProductRepository(EfCoreContext context) : IProductRepository
{
/// <summary>
/// 创建产品
/// </summary>
/// <param name="product">产品</param>
/// <returns></returns>
public Task<int> Create(Product product)
{
context.Products.Add(product);
return context.SaveChangesAsync();
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>ActionResult - Product</returns>
public Task<List<Product>> QueryListById(Guid id)
{
return context.Products.AsNoTracking()
.Include("PCategory")
.Where(product => product.PCategoryId.Equals(id))
.ToListAsync();
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Product</returns>
public Task<Product?> GetById(Guid id)
{
return context.Products.AsNoTracking().FirstOrDefaultAsync(product => product.PId.Equals(id));
}
/// <summary>
/// 根据Id删除产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Task</returns>
public Task<int> DeleteProduct(Guid id)
{
var product = new Product { PId = id };
context.Products.Remove(product);
return context.SaveChangesAsync();
}
/// <summary>
/// 根据分类Id查询产品信息
/// </summary>
/// <param name="cId">分类Id</param>
/// <param name="keyword"></param>
/// <returns>Product</returns>
public Task<List<Product>> QueryByKeyword(Guid cId, string keyword)
{
return context.Products.AsNoTracking()
.Include("PCategory")
.Where(p => p.PCategoryId == cId
&&
p.PTitle != null
&&
p.PTitle.Contains(keyword)
)
.ToListAsync();
}
}

View File

@ -0,0 +1,51 @@
using EFCore_1_Start.Model;
using EFCore_1_Start.Repositories.IRepository;
using EFCore_1_Start.Service.IServices;
namespace EFCore_1_Start.Service;
/// <summary>
/// 产品分类服务
/// </summary>
/// <param name="productRepository">IPCategoryRepository</param>
public class PCategoryService(IPCategoryRepository productRepository) : IPCategoryService
{
/// <summary>
/// 创建产品分类
/// </summary>
/// <param name="pCategory"></param>
/// <returns></returns>
public Task<int> CreatePCategory(PCategory? pCategory)
{
return productRepository.CreatePCategory(pCategory);
}
/// <summary>
/// 获取所有产品信息
/// </summary>
/// <returns></returns>
public List<PCategory> GetAll()
{
return productRepository.GetAll();
}
/// <summary>
/// 根据id查找分类
/// </summary>
/// <param name="id">Id</param>
/// <returns></returns>
public PCategory? GetById(Guid id)
{
return productRepository.GetById(id);
}
/// <summary>
/// 根据Id删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
public Task<int> DeletePCategoryById(Guid? id)
{
return productRepository.DeletePCategoryById(id);
}
}

View File

@ -0,0 +1,35 @@
using EFCore_1_Start.Model;
namespace EFCore_1_Start.Service.IServices;
/// <summary>
/// 产品分类接口服务
/// </summary>
public interface IPCategoryService
{
/// <summary>
/// 创建产品分类
/// </summary>
/// <param name="pCategory"></param>
/// <returns></returns>
Task<int> CreatePCategory(PCategory? pCategory);
/// <summary>
/// 获取所有产品信息
/// </summary>
/// <returns></returns>
List<PCategory> GetAll();
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id"></param>
PCategory? GetById(Guid id);
/// <summary>
/// 根据Id删除
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
Task<int> DeletePCategoryById(Guid? id);
}

View File

@ -0,0 +1,45 @@
using EFCore_1_Start.Model;
namespace EFCore_1_Start.Service.IServices;
/// <summary>
/// Product服务
/// </summary>
public interface IProductService
{
/// <summary>
/// 创建产品
/// </summary>
/// <param name="product">产品</param>
/// <returns></returns>
Task<int> CreateProduct(Product product);
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>ActionResult - Product</returns>
Task<List<Product>> QueryListById(Guid id);
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Product</returns>
Task<Product?> GetById(Guid id);
/// <summary>
/// 根据Id删除产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Task</returns>
Task<int> DeleteProduct(Guid id);
/// <summary>
/// 根据分类Id查询产品信息
/// </summary>
/// <param name="cId">分类Id</param>
/// <param name="keyword"></param>
/// <returns>Product</returns>
Task<List<Product>> QueryByKeyword(Guid cId, string keyword);
}

View File

@ -0,0 +1,73 @@
using EFCore_1_Start.Model;
using EFCore_1_Start.Repositories.IRepository;
using EFCore_1_Start.Service.IServices;
namespace EFCore_1_Start.Service;
/// <summary>
/// 产品服务实现
/// </summary>
public class ProductService : IProductService
{
private readonly IProductRepository _productRepository;
/// <summary>
/// 产品服务构造器
/// </summary>
/// <param name="productRepository">IProductRepository</param>
public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}
/// <summary>
/// 创建产品
/// </summary>
/// <param name="product">产品</param>
/// <returns></returns>
public Task<int> CreateProduct(Product product)
{
return _productRepository.Create(product);
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>ActionResult - Product</returns>
public Task<List<Product>> QueryListById(Guid id)
{
return _productRepository.QueryListById(id);
}
/// <summary>
/// 根据id查询产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Product</returns>
public Task<Product?> GetById(Guid id)
{
return _productRepository.GetById(id);
}
/// <summary>
/// 根据Id删除产品
/// </summary>
/// <param name="id">Id</param>
/// <returns>Task</returns>
public Task<int> DeleteProduct(Guid id)
{
return _productRepository.DeleteProduct(id);
}
/// <summary>
/// 根据分类Id查询产品信息
/// </summary>
/// <param name="cId">分类Id</param>
/// <param name="keyword"></param>
/// <returns>Product</returns>
public Task<List<Product>> QueryByKeyword(Guid cId, string keyword)
{
return _productRepository.QueryByKeyword(cId, keyword);
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BunnyTest": "Server=rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com;Port=3306;User ID=root;Password=0212zkw!;Database=test_product"
}
}

View File

@ -0,0 +1,23 @@
using EFCore_2_JWT.Model;
using Microsoft.EntityFrameworkCore;
namespace EFCore_2_JWT.Context;
/// <summary>
/// EFCore 上下文
/// </summary>
public class EfCoreContext : DbContext
{
/// <summary>
/// EfCoreContext 上下文
/// </summary>
/// <param name="options">DbContextOptions</param>
public EfCoreContext(DbContextOptions options) : base(options)
{
}
/// <summary>
/// 数据库用户集合
/// </summary>
public DbSet<User>? Users { get; set; }
}

View File

@ -0,0 +1,51 @@
using EFCore_2_JWT.Model;
using EFCore_2_JWT.Service.IService;
using Microsoft.AspNetCore.Mvc;
namespace EFCore_2_JWT.Controller;
/// <summary>
/// 用户控制器
/// </summary>
[Route("/api/[controller]/[action]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
/// <summary>
/// UserController构造器
/// </summary>
/// <param name="userService"></param>
public UserController(IUserService userService)
{
_userService = userService;
}
/// <summary>
/// 创建用户令牌
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <returns>创建完成令牌</returns>
[HttpGet]
public async Task<ActionResult> CreateUserToken(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return NotFound();
var userToken = await _userService.CreateUserToken(username, password);
return Ok(userToken);
}
/// <summary>
/// 创建用户实体
/// </summary>
/// <param name="user">用户实体表单</param>
/// <returns>创建成功个数</returns>
[HttpPost]
public async Task<ActionResult<int>> Create([FromBody] User? user)
{
if (user == null) return NotFound();
return await _userService.Create(user);
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>EFCore_2_JWT</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.36"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.36"/>
<PackageReference Include="MySql.EntityFrameworkCore" Version="6.0.33"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,35 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCore_2_JWT.Model;
/// <summary>
/// 用户模型对象
/// </summary>
[Table("users")]
public class User
{
/// <summary>
/// /主键
/// </summary>
[Key]
public Guid? Id { get; set; } = Guid.NewGuid();
/// <summary>
/// 用户名
/// </summary>
[Column("username", TypeName = "varchar(100)")]
public string? Username { get; set; }
/// <summary>
/// 用户密码
/// </summary>
[Column("password", TypeName = "varchar(100)")]
public string? Password { get; set; }
/// <summary>
/// 邮箱
/// </summary>
[Column("email", TypeName = "varchar(100)")]
public string? Email { get; set; }
}

View File

@ -0,0 +1,65 @@
using System.Diagnostics;
using System.Reflection;
using EFCore_2_JWT.Context;
using EFCore_2_JWT.Service;
using EFCore_2_JWT.Service.IService;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
});
});
// 注册DbContext 服务
builder.Services.AddDbContext<EfCoreContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("BunnyTest");
options.UseMySQL(connectionString);
});
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
// ⭐ 添加 XML 注释支持
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// 依赖注入
builder.Services.AddTransient<IUserService, UserService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.MapSwagger();
app.UseSwaggerUI();
}
// 创建数据库
using (var serviceScope = app.Services.CreateScope())
{
var efCoreContext = serviceScope.ServiceProvider.GetRequiredService<EfCoreContext>();
var dbCreated = efCoreContext.Database.EnsureCreatedAsync();
Debug.WriteLine(dbCreated.Result ? "Database created successfully!" : "Database already exists.");
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53710",
"sslPort": 44345
}
},
"profiles": {
"EFCore_2_JWT": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7297;http://localhost:5194",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,24 @@
using EFCore_2_JWT.Model;
namespace EFCore_2_JWT.Service.IService;
/// <summary>
/// 用户服务接口
/// </summary>
public interface IUserService
{
/// <summary>
/// 创建用户实体
/// </summary>
/// <param name="user">用户实体表单</param>
/// <returns>创建成功个数</returns>
Task<int> Create(User user);
/// <summary>
/// 创建用户令牌
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <returns>创建完成令牌</returns>
Task<string> CreateUserToken(string username, string password);
}

View File

@ -0,0 +1,69 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using EFCore_2_JWT.Context;
using EFCore_2_JWT.Model;
using EFCore_2_JWT.Service.IService;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
namespace EFCore_2_JWT.Service;
/// <summary>
/// 用户服务对象
/// </summary>
public class UserService : IUserService
{
private readonly EfCoreContext _context;
/// <summary>
/// 用户服务实现对象
/// </summary>
/// <param name="context"></param>
public UserService(EfCoreContext context)
{
_context = context;
}
/// <summary>
/// 创建用户实体
/// </summary>
/// <param name="user">用户实体表单</param>
/// <returns>创建成功个数</returns>
public Task<int> Create(User user)
{
_context.Users?.Add(user);
return _context.SaveChangesAsync();
}
/// <summary>
/// 创建用户令牌
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <returns>创建完成令牌</returns>
public async Task<string> CreateUserToken(string username, string password)
{
var user = await _context.Users!.AsNoTracking()
.FirstOrDefaultAsync(user =>
user.Username!.Equals(username)
&& user.Password!.Equals(password));
if (user == null) throw new RuntimeBinderException("用户不存在");
var claims = new[]
{
new Claim(ClaimTypes.Sid, user.Id.ToString()!),
new Claim(ClaimTypes.Name, user.Username!)
};
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Bunny4565641145648445564545456454612541"));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var jwtSecurityToken = new JwtSecurityToken("issuer", "audience", claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BunnyTest": "Server=rm-bp12z6hlv46vi6g8mro.mysql.rds.aliyuncs.com;Port=3306;User ID=root;Password=0212zkw!;Database=test_jwt"
}
}

View File

@ -0,0 +1,10 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AConfigurationBuilder_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F4b7902162c67452381f63aa5e0822851ab20_003F66_003F21645c01_003FConfigurationBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACorsPolicyBuilder_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e1d9d5a16334f6e917d67f92e4c287a16910_003F2b_003F3d5df50f_003FCorsPolicyBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJwtSecurityToken_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F2e9cda094f2549d1b29a5c587f060d6912e20_003Ff4_003F4bd84744_003FJwtSecurityToken_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMySqlConnection_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Ff7cba45eb29341ce8553a2eb123e67eed1568_003Fe9_003Fb86ddb92_003FMySqlConnection_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMySQLDbContextOptionsExtensions_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fad9b1b0426e74022a81f18bf1eff6fcc3ba00_003F2d_003F2b3fc9a7_003FMySQLDbContextOptionsExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMySQLServiceCollectionExtensions_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F88b436b38b1e458fa636d93bfea110bd3f068_003F01_003F1d1f8113_003FMySQLServiceCollectionExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AObject_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F2b90705926ae4f10aa01bbe3f1025828c90908_003Fcd_003F73574be5_003FObject_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AServiceProvider_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F58d594d5e8bc4c39adae6aa91c86a8eb16910_003Ff6_003F5bb1f2b1_003FServiceProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
</wpf:ResourceDictionary>

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ASP_1_WebApi</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,6 @@
@ASP_1_WebApi_HostAddress = http://localhost:5076
GET {{ASP_1_WebApi_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@ -0,0 +1,44 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53934",
"sslPort": 44328
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5076",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7295;http://localhost:5076",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>Dialog_ModuleA</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Prism.DryIoc" Version="9.0.537" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<Compile Update="Views\ViewB.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,17 @@
using Dialog_ModuleA.ViewModels;
using Dialog_ModuleA.Views;
namespace Dialog_ModuleA;
public class ModuleAProfile : IModule
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>();
containerRegistry.RegisterForNavigation<ViewC, ViewCViewModel>();
}
public void OnInitialized(IContainerProvider containerProvider)
{
}
}

View File

@ -0,0 +1,5 @@
namespace Dialog_ModuleA.ViewModels;
public class ViewAViewModel : BindableBase
{
}

View File

@ -0,0 +1,43 @@
namespace Dialog_ModuleA.ViewModels;
public class ViewCViewModel : IDialogAware
{
public ViewCViewModel(DialogCloseListener requestClose)
{
RequestClose = requestClose;
ConfirmCommand = new DelegateCommand(Confirm);
SaveCommand = new DelegateCommand(Save);
}
public DelegateCommand ConfirmCommand { get; set; }
public DelegateCommand SaveCommand { get; set; }
public string? Title { get; set; }
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
var keys = new DialogParameters { { "Value", "Hello" } };
RequestClose.Invoke(keys, ButtonResult.OK);
}
public void OnDialogOpened(IDialogParameters parameters)
{
Title = parameters.GetValue<string>("Value");
}
public DialogCloseListener RequestClose { get; }
private void Save()
{
}
private void Confirm()
{
RequestClose.Invoke(new DialogResult(ButtonResult.OK));
}
}

View File

@ -0,0 +1,11 @@
<UserControl x:Class="Dialog_ModuleA.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock FontSize="40" FontWeight="DemiBold" FontStyle="Normal" Background="MintCream" Foreground="Purple">prism</TextBlock>
</Grid>
</UserControl>

View File

@ -0,0 +1,11 @@
using System.Windows.Controls;
namespace Dialog_ModuleA.Views;
public partial class ViewA : UserControl
{
public ViewA()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,24 @@
<UserControl x:Class="Dialog_ModuleA.Views.ViewC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel>
<TextBlock Background="White" Foreground="Red" FontSize="40" FontWeight="DemiBold">弹窗</TextBlock>
<TextBlock FontSize="40" Text="{Binding Title}" />
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Margin="5 0" Padding="44 14" FontSize="19" FontWeight="DemiBold" Command="{Binding ConfirmCommand}">确认</Button>
<Button Margin="5 0" Padding="44 14" FontSize="19" FontWeight="DemiBold" Command="{Binding SaveCommand}">保存</Button>
</StackPanel>
</Grid>
</UserControl>

View File

@ -0,0 +1,14 @@
using System.Windows.Controls;
namespace Dialog_ModuleA.Views;
/// <summary>
/// ViewB.xaml 的交互逻辑
/// </summary>
public partial class ViewC : UserControl
{
public ViewC()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>Dialog_ModuleB</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Prism.DryIoc" Version="9.0.537"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
using Dialog_ModuleB.Views;
namespace Dialog_ModuleB;
public class ModuleBProfile : IModule
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewB>();
}
public void OnInitialized(IContainerProvider containerProvider)
{
}
}

View File

@ -0,0 +1,11 @@
<UserControl x:Class="Dialog_ModuleB.Views.ViewB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock FontSize="40" FontWeight="DemiBold">ModuleB</TextBlock>
</Grid>
</UserControl>

View File

@ -0,0 +1,11 @@
using System.Windows.Controls;
namespace Dialog_ModuleB.Views;
public partial class ViewB : UserControl
{
public ViewB()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,8 @@
<dryIoc:PrismApplication x:Class="Dilog_MainApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dryIoc="http://prismlibrary.com/">
<dryIoc:PrismApplication.Resources>
</dryIoc:PrismApplication.Resources>
</dryIoc:PrismApplication>

View File

@ -0,0 +1,28 @@
using System.Windows;
using Dialog_ModuleA;
using Dialog_ModuleB;
using Dilog_MainApplication.Views;
namespace Dilog_MainApplication;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<ModuleAProfile>();
moduleCatalog.AddModule<ModuleBProfile>();
base.ConfigureModuleCatalog(moduleCatalog);
}
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>Dilog_MainApplication</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Prism.DryIoc" Version="9.0.537" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dialog-ModuleA\Dialog-ModuleA.csproj" />
<ProjectReference Include="..\Dialog-ModuleB\Dialog-ModuleB.csproj" />
</ItemGroup>
<ItemGroup>
<Page Update="Views\MainView.xaml">
<Generator>MSBuild:Compile</Generator>
<XamlRuntime>Wpf</XamlRuntime>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>

View File

@ -0,0 +1,34 @@
namespace Dilog_MainApplication.ViewModels;
public class MainViewModel : BindableBase
{
private readonly IDialogService _dialogService;
private readonly IRegionManager _regionManager;
public MainViewModel(IRegionManager regionManager, IDialogService dialogService)
{
_regionManager = regionManager;
_dialogService = dialogService;
OpenCommand = new DelegateCommand<string>(Open);
OpenDialogCommand = new DelegateCommand<string>(OpenDialog);
}
public DelegateCommand<string> OpenCommand { get; private set; }
public DelegateCommand<string> OpenDialogCommand { get; private set; }
private void OpenDialog(string obj)
{
var keys = new DialogParameters { { "Value", "Hello" } };
_dialogService.ShowDialog(obj, keys, callback =>
{
if (callback.Result != ButtonResult.OK) return;
var value = callback.Parameters.GetValue<string>("Value");
Console.WriteLine(value);
});
}
private void Open(string obj)
{
_regionManager.Regions["MainRegion"].RequestNavigate(obj);
}
}

View File

@ -0,0 +1,33 @@
<Window x:Class="Dilog_MainApplication.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Margin="5 0" FontSize="40" FontWeight="DemiBold" Command="{Binding OpenCommand}"
CommandParameter="ViewA">
模块A
</Button>
<Button Margin="5 0" FontSize="40" FontWeight="DemiBold" Command="{Binding OpenCommand}"
CommandParameter="ViewB">
模块B
</Button>
<Button Margin="5 0" FontSize="40" FontWeight="DemiBold" Command="{Binding OpenDialogCommand}"
CommandParameter="ViewC">
模块C
</Button>
</StackPanel>
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="MainRegion" />
</Grid>
</Window>

View File

@ -0,0 +1,14 @@
using System.Windows;
namespace Dilog_MainApplication.Views;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,8 @@
<dryIoc:PrismApplication x:Class="MainApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dryIoc="http://prismlibrary.com/">
<dryIoc:PrismApplication.Resources>
</dryIoc:PrismApplication.Resources>
</dryIoc:PrismApplication>

View File

@ -0,0 +1,28 @@
using System.Windows;
using MainApplication.Views;
using ModuleA;
using ModuleB;
namespace MainApplication;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<ModuleAProfile>();
moduleCatalog.AddModule<ModuleBProfile>();
base.ConfigureModuleCatalog(moduleCatalog);
}
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Prism.DryIoc" Version="9.0.537"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ModuleA\ModuleA.csproj"/>
<ProjectReference Include="..\ModuleB\ModuleB.csproj"/>
</ItemGroup>
<ItemGroup>
<Page Update="Views\MainView.xaml">
<Generator>MSBuild:Compile</Generator>
<XamlRuntime>Wpf</XamlRuntime>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>

Some files were not shown because too many files have changed in this diff Show More