|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
-
- //
-
-
-
-
-
-
-
-
- namespace SafeCampus.Web.Core;
-
- /// <summary>
- /// 网站开启状态 状态中间件
- /// </summary>
- public class WebStatusMiddleware
- {
- private readonly RequestDelegate _next;
-
- public WebStatusMiddleware(RequestDelegate next)
- {
- _next = next;
- }
-
- public async Task InvokeAsync(HttpContext context)
- {
- var path = context.Request.Path.Value;//获取请求路径
- // 检查请求路径是否以 "/biz" 开头
- if (path.Length > 1 && !path.Contains('.') && !path.StartsWith("/sys", StringComparison.OrdinalIgnoreCase))
- {
- // 通过 context.RequestServices 解析
- var configService = context.RequestServices.GetService<IConfigService>();
- // 获取网站状态
- var webStatus = await configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_WEB_STATUS);
- // 如果网站状态为禁用,则返回 443 状态码
- if (webStatus.ConfigValue == CommonStatusConst.DISABLED)
- {
- context.Response.StatusCode = 423;
- return;
- }
- }
- await _next(context);
- }
- }
-
- /// <summary>
- /// 中间件拓展类
- /// </summary>
- public static class WebStatusMiddlewareExtensions
- {
- public static IApplicationBuilder UseWebStatus(this IApplicationBuilder builder)
- {
- return builder.UseMiddleware<WebStatusMiddleware>();
- }
- }
|