iotgateway/WalkingTec.Mvvm/WalkingTec.Mvvm.Mvc/Helper/WtmMiddleware.cs

80 lines
2.8 KiB
C#
Raw Normal View History

2021-12-14 06:10:44 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Options;
using WalkingTec.Mvvm.Core;
2022-08-23 05:47:00 +00:00
using WalkingTec.Mvvm.Core.Extensions;
2021-12-14 06:10:44 +00:00
namespace WalkingTec.Mvvm.Mvc
{
public class WtmMiddleware
{
private readonly RequestDelegate _next;
public WtmMiddleware(RequestDelegate next)
{
_next = next;
}
2022-08-23 05:47:00 +00:00
public async Task InvokeAsync(HttpContext context, WTMContext wtm)
2021-12-14 06:10:44 +00:00
{
var max = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
if (max.IsReadOnly == false)
{
2022-08-23 05:47:00 +00:00
max.MaxRequestBodySize = wtm.ConfigInfo.FileUploadOptions.UploadLimit;
2021-12-14 06:10:44 +00:00
}
if (context.Request.Path == "/")
{
2022-08-23 05:47:00 +00:00
context.Response.Cookies.Append("pagemode", wtm.ConfigInfo.PageMode.ToString());
context.Response.Cookies.Append("tabmode", wtm.ConfigInfo.TabMode.ToString());
2021-12-14 06:10:44 +00:00
}
if (context.Request.ContentLength > 0 && context.Request.ContentLength < 512000)
{
context.Request.EnableBuffering();
context.Request.Body.Position = 0;
StreamReader tr = new StreamReader(context.Request.Body);
string body = tr.ReadToEndAsync().Result;
context.Request.Body.Position = 0;
if (context.Items.ContainsKey("DONOTUSE_REQUESTBODY") == false)
{
context.Items.Add("DONOTUSE_REQUESTBODY", body);
}
else
{
context.Items["DONOTUSE_REQUESTBODY"] = body;
}
}
2022-08-23 05:47:00 +00:00
//if(wtm.ConfigInfo.Domains != null)
//{
// var mainHost = wtm.ConfigInfo.Domains.Where(x=>x.Key== "mainhost").Select(x=>x.Value.Address).FirstOrDefault();
// if(string.IsNullOrEmpty(mainHost) == false)
// {
// if(context.Request.RouteValues["controller"]?.ToString()?.ToLower() == "login")
// {
// var test = await context.Request.RedirectCall(wtm, "mainhost");
// }
// }
//}
2021-12-14 06:10:44 +00:00
await _next(context);
if (context.Response.StatusCode == 404)
{
await context.Response.WriteAsync(string.Empty);
}
}
}
public static class WtmMiddlewareExtensions
{
public static IApplicationBuilder UseWtm(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<WtmMiddleware>();
}
}
}