Mobile Application Development Course Assignment - Group 7
dotnet add package Swashbuckle.AspNetCore
dotnet restore1. Execute UserDB.sql
MySQL Workbench:
- Open MySQL Workbench and connect to your server
- Open
UserDB.sqlfrom project root - Click Execute (⚡) or press
Ctrl+Shift+Enter
Command Line:
mysql -u root -p < UserDB.sql2. Configure Connection
Update password in Repository\Constants.cs:
public static string CONNECTION_STRING = @"server=localhost;uid=root;pwd=YOUR_PASSWORD;database=user";- Open
Gp7_CA.slnin Visual Studio - Restore packages (right-click solution → Restore NuGet Packages)
- Press
F5to run - Application:
http://localhost:5107 - Swagger UI:
http://localhost:5107/swagger
1. User Authentication
POST http://localhost:5107/User/Authenticate
// Request
{
"username": "testuser",
"password": "testpass"
}
// Response (200 OK)
{
"success": true,
"message": "Welcome testuser",
"userId": 1,
"isPaidUser": false,
"completionTime": 0
}2. Update Completion Time
POST http://localhost:5107/User/UpdateCompletionTime
// Request
{
"userId": 1,
"completionTime": 45.5
}
// Response (200 OK)
{
"success": true,
"message": "Completion time updated successfully"
}3. Get Leaderboard
GET http://localhost:5107/User/Leaderboard?limit=10
// Response (200 OK)
{
"success": true,
"count": 2,
"leaderboard": [
{"username": "user1", "completionTime": 30.5, "isPaidUser": false},
{"username": "user2", "completionTime": 45.2, "isPaidUser": true}
]
}Note: All POST requests require Content-Type: application/json header.
| Issue | Solution |
|---|---|
| Database Connection Failed | Check MySQL is running and password in Constants.cs is correct |
| NullReferenceException (API) | Set Content-Type: application/json header in request |
| Table Not Found | Run UserDB.sql to create database and tables |
| Swagger UI Not Appearing | Install Swashbuckle.AspNetCore package and verify Program.cs configuration |
| ViewData NullReferenceException | Remove @page and @model PageModel directives from views in /Views folder |
dotnet add package Swashbuckle.AspNetCore
dotnet restore1. 执行UserDB.sql
MySQL Workbench:
- 打开MySQL Workbench并连接到服务器
- 从项目根目录打开
UserDB.sql - 点击执行(⚡)或按
Ctrl+Shift+Enter
命令行:
mysql -u root -p < UserDB.sql2. 配置连接
在Repository\Constants.cs中更新密码:
public static string CONNECTION_STRING = @"server=localhost;uid=root;pwd=YOUR_PASSWORD;database=user";- 在Visual Studio中打开
Gp7_CA.sln - 恢复包(右键解决方案 → 恢复NuGet包)
- 按
F5运行 - 应用程序:
http://localhost:5107 - Swagger UI:
http://localhost:5107/swagger
1. 用户认证
POST http://localhost:5107/User/Authenticate
// 请求
{
"username": "testuser",
"password": "testpass"
}
// 响应 (200 OK)
{
"success": true,
"message": "Welcome testuser",
"userId": 1,
"isPaidUser": false,
"completionTime": 0
}2. 更新完成时间
POST http://localhost:5107/User/UpdateCompletionTime
// 请求
{
"userId": 1,
"completionTime": 45.5
}
// 响应 (200 OK)
{
"success": true,
"message": "Completion time updated successfully"
}3. 获取排行榜
GET http://localhost:5107/User/Leaderboard?limit=10
// 响应 (200 OK)
{
"success": true,
"count": 2,
"leaderboard": [
{"username": "user1", "completionTime": 30.5, "isPaidUser": false},
{"username": "user2", "completionTime": 45.2, "isPaidUser": true}
]
}注意: 所有POST请求需要Content-Type: application/json头。
| 问题 | 解决方案 |
|---|---|
| 数据库连接失败 | 检查MySQL正在运行且Constants.cs中的密码正确 |
| 空引用异常 (API) | 在请求中设置Content-Type: application/json头 |
| 找不到表 | 运行UserDB.sql创建数据库和表 |
| Swagger UI未显示 | 安装Swashbuckle.AspNetCore包并验证Program.cs配置 |
| ViewData空引用异常 | 从/Views文件夹中的视图删除@page和@model PageModel指令 |