using Apewer.Web;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using static Apewer.NumberUtility;
using static Apewer.Web.ApiUtility;
namespace Apewer.AspNetBridge
{
/// 桥接 ASP.NET 的控制器。
public class BridgeController : Web.ApiController
{
#region static routes
static RouteItem[] _routes = null;
/// 导出所有路由项。
public static Json ExportRoutes()
{
var array = Json.NewArray();
if (_routes != null)
{
foreach (var route in _routes)
{
var item = route.ToJson();
array.AddItem(item);
}
}
return array;
}
/// 初始化路由。
/// 包含控制器的程序集。
/// 包含返回 System.Void 的方法。
public static void Initialize(IEnumerable assemblies, bool withVoid = false) => _routes = RouteItem.Parse(assemblies, withVoid);
/// 初始化路由,不包含返回 System.Void 的方法。
/// 包含控制器的程序集。
public static void Initialize(params Assembly[] assemblies) => _routes = RouteItem.Parse(assemblies, false);
#endregion
#region events
/// 发生异常时候的处理程序。
public static OnException OnException { get; set; }
/// 输出异常。
///
public static void Output(ApiRequest request, ApiResponse response, MethodInfo method, Exception exception)
{
if (request == null) throw new ArgumentNullException(nameof(request));
if (response == null) throw new ArgumentNullException(nameof(response));
if (method == null) throw new ArgumentNullException(nameof(method));
if (exception == null) throw new ArgumentNullException(nameof(exception));
var sb = new StringBuilder();
if (request.IP.NotEmpty())
{
sb.Append(request.IP);
sb.Append(" ");
}
sb.Append(request.Method.ToString());
if (request.Url != null)
{
sb.Append(" ");
sb.Append(request.Url.OriginalString);
}
if (method != null)
{
sb.Append("\r\n");
sb.Append(method.DeclaringType.FullName);
sb.Append(".");
sb.Append(method.Name);
}
sb.Append("\r\n\r\n");
sb.Append(new ApiExceptionModel(exception).ToString());
var model = new ApiTextModel(sb.ToString());
model.Status = 500;
response.Model = model;
}
/// 使用格式化程序处理所有响应。
///
/// 默认值:False( Bridge )
/// True:所有响应均使用自定义格式化程序,忽略 Bridge 内置程序。
/// False:优先使用 Bridge 内置程序格式化,对不支持的类型使用自定义程序。
///
public static bool FormatAll { get; set; }
/// 自定义格式化程序。
public static Func