增加Controller API模式

增加图片缓存API
This commit is contained in:
chfei committed 2026-09-14 15:48:11 +08:00
1 parent 8ee4bf59b9
commit e48ee01201
16 files changed
+906 -170

No files matched your search

+237 -156
View File
@@ -1,165 +1,246 @@
using KJD_Web.Utils;
using Autofac;
using Autofac.Integration.WebApi;
using FreeSql;
using KJD_Web.Utils;
using MySql.Data.MySqlClient;
using MAX.Core.DependencyInjection;
using NLog;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using System.Web.SessionState;
namespace KJD_Web
{
public class Global : System.Web.HttpApplication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
LogHelper.Info("应用程序启动");
var container = BuildContainer();
// 让 Web API 用 Autofac 解析 Controller
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Session_Start(object sender, EventArgs e)
{
internal static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
var appAssembly = Assembly.GetExecutingAssembly();
}
// ImageCacheService 使用独立的 imagecache 数据库。
builder.Register(_ => CreateImageCacheFreeSql())
.As<IFreeSql>()
.SingleInstance();
protected void Application_BeginRequest(object sender, EventArgs e)
{
string hPath = Request.Url.LocalPath.ToString();
if (!hPath.Contains(".asmx") && hPath.Contains("/user/"))
{
hPath = hPath.Replace("/user/", "/UserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/shop/"))
{
hPath = hPath.Replace("/shop/", "/ShopService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
{
hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/order/"))
{
hPath = hPath.Replace("/order/", "/OrderService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/product/"))
{
hPath = hPath.Replace("/product/", "/ProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/product_third/"))
{
hPath = hPath.Replace("/product_third/", "/ProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/price/"))
{
hPath = hPath.Replace("/price/", "/PriceService.asmx/");
Context.RewritePath(hPath);
}
// 注册 Web API 所有 Controller
builder.RegisterApiControllers(appAssembly);
#region 注册所有IOC接口
Type typeSingleton = typeof(ISingletonDependency);
builder.RegisterAssemblyTypes(appAssembly)
.Where(a =>
typeSingleton.IsAssignableFrom(a)
&& a.IsClass
&& !a.IsAbstract
)
.AsImplementedInterfaces()
.AsSelf()
.SingleInstance();
Type typeTransient = typeof(ITransientDependency);
builder.RegisterAssemblyTypes(appAssembly)
.Where(a =>
typeTransient.IsAssignableFrom(a)
&& a.IsClass
&& !a.IsAbstract
)
.AsImplementedInterfaces()
.AsSelf()
.InstancePerDependency();
Type typeScoped = typeof(IScopedDependency);
builder.RegisterAssemblyTypes(appAssembly)
.Where(a => typeScoped.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract)
.AsImplementedInterfaces()
.AsSelf()
.InstancePerRequest();
#endregion
return builder.Build();
}
private static IFreeSql CreateImageCacheFreeSql()
{
var connection = ConfigurationManager.ConnectionStrings["ConnKJD"];
if (connection == null || string.IsNullOrWhiteSpace(connection.ConnectionString))
{
throw new ConfigurationErrorsException("未配置连接字符串:ConnKJD");
}
var connectionBuilder = new MySqlConnectionStringBuilder(connection.ConnectionString)
{
Database = "imagecache"
};
return new FreeSqlBuilder()
.UseConnectionString(DataType.MySql, connectionBuilder.ConnectionString)
.UseAutoSyncStructure(false)
.Build();
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
string hPath = Request.Url.LocalPath.ToString();
if (!hPath.Contains(".asmx") && hPath.Contains("/user/"))
{
hPath = hPath.Replace("/user/", "/UserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/shop/"))
{
hPath = hPath.Replace("/shop/", "/ShopService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
{
hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/order/"))
{
hPath = hPath.Replace("/order/", "/OrderService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/product/"))
{
hPath = hPath.Replace("/product/", "/ProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/product_third/"))
{
hPath = hPath.Replace("/product_third/", "/ProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/price/"))
{
hPath = hPath.Replace("/price/", "/PriceService.asmx/");
Context.RewritePath(hPath);
}
if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_user/"))
{
hPath = hPath.Replace("/bxx_user/", "/BXXUserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_shop/"))
{
hPath = hPath.Replace("/bxx_shop/", "/BXXShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_product/"))
{
hPath = hPath.Replace("/bxx_product/", "/BXXProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_product_third/"))
{
hPath = hPath.Replace("/bxx_product_third/", "/BXXProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_price/"))
{
hPath = hPath.Replace("/bxx_price/", "/BXXPriceService.asmx/");
Context.RewritePath(hPath);
}
if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_user/"))
{
hPath = hPath.Replace("/bxx_user/", "/BXXUserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_shop/"))
{
hPath = hPath.Replace("/bxx_shop/", "/BXXShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_product/"))
{
hPath = hPath.Replace("/bxx_product/", "/BXXProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_product_third/"))
{
hPath = hPath.Replace("/bxx_product_third/", "/BXXProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/bxx_price/"))
{
hPath = hPath.Replace("/bxx_price/", "/BXXPriceService.asmx/");
Context.RewritePath(hPath);
}
if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_user/"))
{
hPath = hPath.Replace("/dyxd_user/", "/DYUserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_shop/"))
{
hPath = hPath.Replace("/dyxd_shop/", "/DYShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_product/"))
{
hPath = hPath.Replace("/dyxd_product/", "/DYProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_product_third/"))
{
hPath = hPath.Replace("/dyxd_product_third/", "/DYProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_price/"))
{
hPath = hPath.Replace("/dyxd_price/", "/DYPriceService.asmx/");
Context.RewritePath(hPath);
}
if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_user/"))
{
hPath = hPath.Replace("/dyxd_user/", "/DYUserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_shop/"))
{
hPath = hPath.Replace("/dyxd_shop/", "/DYShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_product/"))
{
hPath = hPath.Replace("/dyxd_product/", "/DYProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_product_third/"))
{
hPath = hPath.Replace("/dyxd_product_third/", "/DYProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/dyxd_price/"))
{
hPath = hPath.Replace("/dyxd_price/", "/DYPriceService.asmx/");
Context.RewritePath(hPath);
}
if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_user/"))
{
hPath = hPath.Replace("/wxxd_user/", "/WXUserService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_shop/"))
{
hPath = hPath.Replace("/wxxd_shop/", "/WXShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_product/"))
{
hPath = hPath.Replace("/wxxd_product/", "/WXProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_product_third/"))
{
hPath = hPath.Replace("/wxxd_product_third/", "/WXProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_price/"))
{
hPath = hPath.Replace("/wxxd_price/", "/WXPriceService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_order/"))
{
hPath = hPath.Replace("/wxxd_order/", "/WXOrderService.asmx/");
Context.RewritePath(hPath);
if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_user/"))
{
hPath = hPath.Replace("/wxxd_user/", "/WXUserService.asmx/");
Context.RewritePath(hPath, false);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_shop/"))
{
hPath = hPath.Replace("/wxxd_shop/", "/WXShopService.asmx/");
Context.RewritePath(hPath);
}
//else if (!hPath.Contains(".asmx") && hPath.Contains("/update/"))
//{
// hPath = hPath.Replace("/update/", "/UpdateService.asmx/");
// Context.RewritePath(hPath);
//}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_product/"))
{
hPath = hPath.Replace("/wxxd_product/", "/WXProductService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_product_third/"))
{
hPath = hPath.Replace("/wxxd_product_third/", "/WXProductThirdService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_price/"))
{
hPath = hPath.Replace("/wxxd_price/", "/WXPriceService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_order/"))
{
hPath = hPath.Replace("/wxxd_order/", "/WXOrderService.asmx/");
Context.RewritePath(hPath);
}
else if (!hPath.Contains(".asmx") && hPath.Contains("/wxxd_shop_revenue_statistics/"))
{
@@ -170,30 +251,30 @@ namespace KJD_Web
//try { ApiRequestLogger.BeginRequest(Context); } catch { }
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
}
protected void Application_EndRequest(object sender, EventArgs e)
{
// 请求录制:合并响应内容并落盘
//try { ApiRequestLogger.EndRequest(Context); } catch { }
}
protected void Application_EndRequest(object sender, EventArgs e)
{
// 请求录制:合并响应内容并落盘
//try { ApiRequestLogger.EndRequest(Context); } catch { }
}
protected void Application_Error(object sender, EventArgs e)
{
protected void Application_Error(object sender, EventArgs e)
{
}
}
protected void Session_End(object sender, EventArgs e)
{
protected void Session_End(object sender, EventArgs e)
{
}
}
protected void Application_End(object sender, EventArgs e)
{
protected void Application_End(object sender, EventArgs e)
{
}
}
}
}
}