ASP.NET MVC API 接口验证的示例代码(2)

using DotNet.Utilities; using DotNet.Tracking.API.Common; using DotNet.Tracking.API.Controllers; using DotNet.Tracking.API.Models; /// <summary> /// ApiErrorHandleAttribute /// 全局异常处理 /// /// 修改纪录 /// /// 2016-10-31 版本:1.0 宋彪 创建文件。 /// /// <author> /// <name>宋彪</name> /// <date>2016-10-31</date> /// </author> /// </summary> public class ApiErrorHandleAttribute : System.Web.Http.Filters.ExceptionFilterAttribute { /// <summary> /// 异常统一处理 /// </summary> /// <param></param> public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext) { base.OnException(actionExecutedContext); // 取得发生例外时的错误讯息 var errorMessage = actionExecutedContext.Exception.Message; // 异常记录 string parameters = APIOperateContext.GetRequestParameters(); NLogHelper.Trace(actionExecutedContext.Exception, BaseSystemInfo.SystemCode + " ApiErrorHandleAttribute OnException 完整的请求地址及参数 : " + parameters); // 2016-11-01 加入异常邮件提醒 NLogHelper.InfoMail(actionExecutedContext.Exception, BaseSystemInfo.SystemCode + " ApiErrorHandleAttribute OnException 完整的请求地址及参数 : " + parameters); var result = new ApiResultModel() { Status = HttpStatusCode.BadRequest, ErrorMessage = errorMessage }; // 重新打包回传的讯息 actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.Status, result); } }

5、接口操作的上下文

using DotNet.Business; using DotNet.Model; using DotNet.Utilities; /// <summary> /// APIOperateContext /// 接口操作的上下文 /// 跟上下文有关的一些通用的东西放在这里处理 /// /// 修改纪录 /// /// 2016-10-31 版本:1.0 宋彪 创建文件。 /// /// <author> /// <name>宋彪</name> /// <date>2016-10-31</date> /// </author> /// </summary> public class APIOperateContext { /// <summary> /// 获取当前 操作上下文 (为每个处理浏览器请求的服务器线程 单独创建 操作上下文) /// </summary> public static APIOperateContext Current { get { APIOperateContext oContext = CallContext.GetData(typeof(APIOperateContext).Name) as APIOperateContext; if (oContext == null) { oContext = new APIOperateContext(); CallContext.SetData(typeof(APIOperateContext).Name, oContext); } return oContext; } } #region Http上下文 及 相关属性 /// <summary> /// Http上下文 /// </summary> public HttpContext ContextHttp { get { return HttpContext.Current; } } /// <summary> /// 输出对象 /// </summary> public HttpResponse Response { get { return ContextHttp.Response; } } /// <summary> /// 请求对象 /// </summary> public HttpRequest Request { get { return ContextHttp.Request; } } /// <summary> /// Session对象 /// </summary> System.Web.SessionState.HttpSessionState Session { get { return ContextHttp.Session; } } #endregion /// <summary> /// 获取全部请求参数,get和post的 简化版 /// </summary> public static string GetRequestParameters() { string query = HttpContext.Current.Request.Url.Query; NameValueCollection nvc; string baseUrl; ParseUrl(query, out baseUrl, out nvc); List<string> list = new List<string>() { }; foreach (var key in nvc.AllKeys) { list.Add(key + "=" + nvc[key]); } var form = HttpContext.Current.Request.Form; foreach (var key in form.AllKeys) { list.Add(key + "=" + form[key]); } string result = HttpContext.Current.Request.Url.AbsoluteUri + "?" + string.Join("&", list); return result; } /// <summary> /// 分析 url 字符串中的参数信息 /// 针对get请求的 /// </summary> /// <param>输入的 URL</param> /// <param>输出 URL 的基础部分</param> /// <param>输出分析后得到的 (参数名,参数值) 的集合</param> public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc) { if (url == null) { throw new ArgumentNullException("url"); } nvc = new NameValueCollection(); baseUrl = ""; if (url == "") { return; } int questionMarkIndex = url.IndexOf('?'); if (questionMarkIndex == -1) { baseUrl = url; return; } baseUrl = url.Substring(0, questionMarkIndex); if (questionMarkIndex == url.Length - 1) { return; } string ps = url.Substring(questionMarkIndex + 1); // 开始分析参数对 Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled); MatchCollection mc = re.Matches(ps); foreach (Match m in mc) { nvc.Add(m.Result("$2").ToLower(), m.Result("$3")); } } /// <summary> /// 系统编号 /// </summary> public string SystemCode { get { return Request["systemCode"] ?? "Base"; } } /// <summary> /// 权限编号 /// </summary> public string PermissionCode { get { return Request["permissionCode"]; } } /// <summary> /// 访问接口的应用传来AppKey /// </summary> public string AppKey { get { return Request["appKey"]; } } /// <summary> /// 访问接口的应用传来AppSecret /// </summary> public string AppSecret { get { return Request["appSecret"]; } } private BaseUserInfo _userInfo = null; /// <summary> /// 获取当前用户 /// 通过接口AppKey和AppSecret获取的用户 /// </summary> /// <returns></returns> public BaseUserInfo UserInfo { get { BaseUserInfo userInfo = null; BaseUserEntity userEntity = BaseUserManager.GetObjectByCodeByCache(AppKey); if (userEntity != null) { if (BaseServicesLicenseManager.CheckServiceByCache(userEntity.Id, AppSecret)) { userInfo = new BaseUserInfo(); userInfo.Id = userEntity.Id; userInfo.RealName = userEntity.RealName; userInfo.UserName = userEntity.UserName; userInfo.IPAddress = Utilities.GetIPAddress(true); } } return userInfo; } } #region 业务库连接 /// <summary> /// 业务库连接 /// </summary> public static IDbHelper BusinessDbHelper { get { return DbHelperFactory.GetHelper(BaseSystemInfo.BusinessDbType, BaseSystemInfo.BusinessDbConnection); } } #endregion #region 用户中心库连接 /// <summary> /// 用户中心库连接 /// </summary> public static IDbHelper UserCenterDbHelper { get { return DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection); } } #endregion }

7、统一回传格式实体

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpwdw.html