diff --git a/.gitignore b/.gitignore
index ed6d1d2..779fe11 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,9 @@
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
+package-lock.json
+*/wwwroot/dist
+
# User-specific files
*.rsuser
*.suo
diff --git a/WebApplication1.slnx b/WebApplication1.slnx
new file mode 100644
index 0000000..549d235
--- /dev/null
+++ b/WebApplication1.slnx
@@ -0,0 +1,3 @@
+
+
+
diff --git a/WebApplication1/Controllers/ApiController.cs b/WebApplication1/Controllers/ApiController.cs
new file mode 100644
index 0000000..5002cbe
--- /dev/null
+++ b/WebApplication1/Controllers/ApiController.cs
@@ -0,0 +1,21 @@
+using Microsoft.AspNetCore.Mvc;
+
+namespace WebApplication1.Controllers;
+
+[Route("api")]
+[ApiController]
+[ApiExceptionFilter]
+public class ApiController : SiteControllerBase
+{
+ [Route("detail")]
+ [HttpGet]
+ [ResponseCache(CacheProfileName = "NoCache")]
+ public ApiResult GetDetail(int id)
+ {
+ //throw new PermissionException(500, errorMessage: "test error1");
+ //throw new ApiException(12, "test api exception.");
+ return new DetailItem(id, "Test title");
+ }
+}
+
+public record DetailItem(int Id, string Name);
diff --git a/WebApplication1/Controllers/ApiController.filter.cs b/WebApplication1/Controllers/ApiController.filter.cs
new file mode 100644
index 0000000..379971b
--- /dev/null
+++ b/WebApplication1/Controllers/ApiController.filter.cs
@@ -0,0 +1,127 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace WebApplication1.Controllers;
+
+///
+/// API 异常过滤器
+///
+class ApiExceptionFilterAttribute : ExceptionFilterAttribute
+{
+ public ApiExceptionFilterAttribute()
+ {
+ Order = 1;
+ }
+
+ public override void OnException(ExceptionContext context)
+ {
+ var ex = context.Exception;
+
+ if (ex is PermissionException pex)
+ {
+ // 如果为权限异常则修改返回状态码
+ context.HttpContext.Response.StatusCode = pex.StatusCode;
+ }
+
+ ExceptionResult result;
+ if (ex is ApiException aex)
+ {
+#if DEBUG
+ // 调试环境带上堆栈信息
+ result = new ExceptionResult(aex.ErrorCode, aex.ToString());
+#else
+ result = new ExceptionResult(aex);
+#endif
+ }
+ else
+ {
+ result = new ExceptionResult(ex);
+ }
+
+ context.Result = new JsonResult(result);
+ context.ExceptionHandled = true;
+ }
+
+ public override Task OnExceptionAsync(ExceptionContext context)
+ {
+ OnException(context);
+ return Task.CompletedTask;
+ }
+}
+
+///
+/// 页面异常过滤器
+///
+class PageExceptionFilterAttribute : ExceptionFilterAttribute
+{
+ public override void OnException(ExceptionContext context)
+ {
+ OnExceptionAsync(context).Wait();
+ }
+
+ public override async Task OnExceptionAsync(ExceptionContext context)
+ {
+ var ex = context.Exception;
+ int statusCode = 500;
+ int errorCode = -1;
+ string errorMessage;
+ switch (ex)
+ {
+ case ApiException aex:
+ if (aex is PermissionException pex)
+ {
+ // 如果为权限异常则修改返回状态码
+ statusCode = pex.StatusCode;
+ }
+ errorCode = aex.ErrorCode;
+ errorMessage = errorCode switch
+ {
+ ApiException.AUTHORIZED_ERROR => "You are not authorized yet.",
+ ApiException.PARAMETER_ERROR => "The server could not understand the request due to invalid parameters.",
+ ApiException.PERMISSION_ERROR => "You have no right to access this page.",
+ _ => "An unknown error occurred."
+ };
+ break;
+
+ case System.Net.WebException:
+ case EndOfStreamException:
+ errorMessage = "Network connection is failed.";
+ break;
+
+ case TaskCanceledException:
+ case OperationCanceledException:
+ errorMessage = "Operation timeout, maybe caused by the network, please try again later.";
+ break;
+
+ default:
+ errorMessage = "An unknown error occurred.";
+ break;
+ }
+
+ // deprecated: relative path
+ //var prefix = context.HttpContext.Request.Path.Value?.Trim('/');
+ //if (!string.IsNullOrEmpty(prefix))
+ //{
+ // int segmentCount = prefix.Split('/', StringSplitOptions.RemoveEmptyEntries).Length;
+ // prefix = string.Concat(Enumerable.Repeat("../", segmentCount));
+ //}
+
+ var helper = context.HttpContext.RequestServices.GetRequiredService();
+ var html = await helper.RenderAsync(
+ context.HttpContext.Request.PathBase,
+ "Templates/error.html",
+ "src/pages/error.ts",
+ new ApiErrorResult(errorCode, errorMessage),
+ new() { ["Title"] = "Error Page" });
+
+ context.Result = new ContentResult
+ {
+ Content = html,
+ ContentType = "text/html; charset=utf-8",
+ StatusCode = statusCode
+ };
+ context.ExceptionHandled = true;
+ }
+}
+
+record ApiErrorResult(int ErrorCode, string ErrorMessage);
diff --git a/WebApplication1/Controllers/ApiController.model.cs b/WebApplication1/Controllers/ApiController.model.cs
new file mode 100644
index 0000000..7f83c98
--- /dev/null
+++ b/WebApplication1/Controllers/ApiController.model.cs
@@ -0,0 +1,59 @@
+namespace WebApplication1.Controllers;
+
+///
+/// 异常结果类
+///
+public class ExceptionResult : ApiResult