59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace YLErp.Web.App
|
|
{
|
|
public class StaticUrlMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public StaticUrlMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public Task Invoke(HttpContext context)
|
|
{
|
|
var _wwwrootDirs = WwwrootDirsManager.GetWwwrootDirs();
|
|
var path = context.Request.Path.Value;
|
|
if (_wwwrootDirs.TryGetValue(path.ToLower(), out string targetPath))
|
|
{
|
|
context.Request.Path = targetPath;
|
|
}
|
|
|
|
return _next(context);
|
|
}
|
|
}
|
|
|
|
public class WwwrootDirsManager
|
|
{
|
|
private readonly static Dictionary<string,string> _wwwrootDirs;
|
|
private readonly static string rootDir;
|
|
static WwwrootDirsManager()
|
|
{
|
|
rootDir= Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
|
|
_wwwrootDirs = GetFiles(rootDir).ToDictionary(k=>k.ToLower(),v=>v);
|
|
}
|
|
|
|
public static Dictionary<string, string> GetWwwrootDirs()
|
|
{
|
|
return _wwwrootDirs;
|
|
}
|
|
|
|
|
|
static List<string> GetFiles(string directory, string pattern = "*.*")
|
|
{
|
|
List<string> files = new List<string>();
|
|
foreach (var item in Directory.GetFiles(directory, pattern))
|
|
{
|
|
var targetfile= item.Replace(rootDir,"").Replace("\\", "/");
|
|
files.Add(targetfile);
|
|
}
|
|
foreach (var item in Directory.GetDirectories(directory))
|
|
{
|
|
files.AddRange(GetFiles(item, pattern));
|
|
}
|
|
return files;
|
|
}
|
|
}
|
|
}
|