简化WEB库
This commit is contained in:
@@ -1,126 +0,0 @@
|
||||
using Beginor.Owin.StaticFile;
|
||||
using JinianNet.JNTemplate;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Owin;
|
||||
using Microsoft.Owin.Hosting;
|
||||
using Owin;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
using 电子展板.Utility.Core;
|
||||
using 电子展板.Utility.Extension;
|
||||
using 电子展板.Utility.Logs;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace 电子展板
|
||||
{
|
||||
public class WebServer
|
||||
{
|
||||
private static IDisposable host;
|
||||
public static void Start()
|
||||
{
|
||||
LogHelper.Instance.Info("正在开启Web服务");
|
||||
StartOptions startOptions = new StartOptions();
|
||||
startOptions.Urls.Add($"http://*:80/");
|
||||
host = WebApp.Start<Startup>(startOptions);
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
if (host == null)
|
||||
return;
|
||||
LogHelper.Instance.Info("正在停止Web服务");
|
||||
host.Dispose();
|
||||
host = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Web启动类
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
private HttpConfiguration _config;
|
||||
/// <summary>
|
||||
/// 配置
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="env"></param>
|
||||
public void Configuration(IAppBuilder app)
|
||||
{
|
||||
_config = new HttpConfiguration();
|
||||
_config.MapHttpAttributeRoutes();
|
||||
_config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
|
||||
_config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json"));
|
||||
//全局拦截
|
||||
app.Use<ResouceStaticRoute>("/static", "/Assets");
|
||||
app.UseWebApi(_config);
|
||||
//配置模板引擎
|
||||
Engine.Configure(c =>
|
||||
{
|
||||
c.TagPrefix = "${";
|
||||
c.TagSuffix = "}";
|
||||
c.TagFlag = '^';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class ResouceStaticRoute : OwinMiddleware
|
||||
{
|
||||
private string route;
|
||||
private string resources;
|
||||
public ResouceStaticRoute(OwinMiddleware next, string route, string resources) : base(next)
|
||||
{
|
||||
this.route = route;
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
public override async Task Invoke(IOwinContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
string method = context.Request.Method;//GET POST
|
||||
string path = context.Request.Path.ToString();
|
||||
if (method.ToUpper() == "GET" && path.StartsWith(route))
|
||||
{
|
||||
string resourePath = path.Replace(route, "");
|
||||
string packUri = $"pack://application:,,,{resources}{resourePath}";
|
||||
byte[] bytes = null;
|
||||
using (Stream stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream)
|
||||
{
|
||||
bytes = new byte[stream.Length];
|
||||
stream.Read(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
context.Response.ContentType = System.Web.MimeMapping.GetMimeMapping(path);
|
||||
context.Response.Write(bytes, 0, bytes.Length);
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
await Next.Invoke(context);
|
||||
}
|
||||
catch (InvalidCastException ex)
|
||||
{
|
||||
await Next.Invoke(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user