-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (114 loc) · 4.14 KB
/
Program.cs
File metadata and controls
136 lines (114 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Microsoft.EntityFrameworkCore;
using ArticleAPI.Data;
using ArticleAPI.Services;
using ArticleAPI.Models;
var builder = WebApplication.CreateBuilder(args);
// 添加服务到容器
builder.Services.AddControllers();
// 添加 Blazor Server 服务
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// 配置Entity Framework - 统一使用SQLite
builder.Services.AddDbContext<ArticleDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// 添加 HttpContextAccessor
builder.Services.AddHttpContextAccessor();
// 配置HttpClient用于调用API
builder.Services.AddHttpClient<ArticleService>();
// 配置Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() {
Title = "文章管理系统 API",
Version = "v1.0",
Description = "一个基于 ASP.NET Core 8 和 SQLite 的文章管理系统 API",
Contact = new() {
Name = "Morieity",
Email = "morieityqaq@gmail.com"
}
});
// 启用XML注释
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
// 为 Swagger UI 添加更多配置
c.DescribeAllParametersInCamelCase();
});
// 配置CORS(如果需要)
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// 配置HTTP请求管道
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "文章管理系统 API V1");
c.RoutePrefix = "swagger"; // 将Swagger UI移动到 /swagger 路径
});
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
// 在生产环境也启用 Swagger (可选 - 用于演示)
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "文章管理系统 API V1");
c.RoutePrefix = "swagger";
});
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCors("AllowAll");
app.UseRouting();
app.UseAuthorization();
// 配置路由
app.MapControllers(); // API 控制器
app.MapBlazorHub(); // Blazor Hub
app.MapFallbackToPage("/_Host"); // Blazor 页面
// 初始化数据库并添加示例数据
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ArticleDbContext>();
context.Database.EnsureCreated();
// 添加示例数据
if (!context.Articles.Any())
{
context.Articles.AddRange(
new ArticleAPI.Models.Article
{
Title = "欢迎使用 ASP.NET Core Web API",
Content = "这是一个使用 ASP.NET Core 8 和 Entity Framework Core 构建的文章管理 API。通过这个API,您可以轻松地管理文章内容,包括创建、读取、更新和删除操作。",
CreatedAt = DateTime.UtcNow.AddDays(-2)
},
new ArticleAPI.Models.Article
{
Title = "Entity Framework Core 入门指南",
Content = "Entity Framework Core 是一个轻量级、可扩展、开源和跨平台的数据访问技术。它支持 LINQ 查询、变更跟踪、更新和架构迁移。EF Core 可在 .NET Core 或 .NET Framework 上运行。",
CreatedAt = DateTime.UtcNow.AddDays(-1)
},
new ArticleAPI.Models.Article
{
Title = "Blazor Server 应用开发",
Content = "Blazor Server 是一种托管模型,其中组件在服务器上运行,UI 更新通过 SignalR 连接发送到浏览器。这种模式提供了丰富的交互性,同时保持了较小的客户端大小。",
CreatedAt = DateTime.UtcNow.AddHours(-6)
}
);
context.SaveChanges();
}
}
app.Run();