示例#1
0
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var requestId = DotNet.Snowflake.NewId();

            _ = OnBeginLog(requestId, context.HttpContext.Request.Path.ToString(), context.ActionArguments, context.HttpContext.Connection.RemoteIpAddress.ToString(), context);
            var resultContext = await next();

            object resultObj = null;

            if (resultContext.Exception != null)
            {
                resultContext.ExceptionHandled = true;
                resultContext.Result           = new JsonResult(resultObj = new DotNet.Result()
                {
                    Code = 500, Message = resultContext.Exception.Message
                });
            }
            if (resultContext.Result is ObjectResult objectResult)
            {
                resultObj = objectResult.Value;
            }
            else if (resultContext.Result is JsonResult jsonResult)
            {
                resultObj = jsonResult.Value;
            }
            if (!resultObj.IsNull())
            {
                if (!(resultObj is DotNet.Result))
                {
                    resultContext.Result = new JsonResult(resultObj);
                }
                _ = OnEndLog(requestId, context.HttpContext.Request.Path.ToString(), context.ActionArguments, resultObj, context.HttpContext.Connection.RemoteIpAddress.ToString(), context);
            }
        }
示例#2
0
 /// <summary>
 /// 当收到请求时发生。
 /// </summary>
 /// <param name="request"></param>
 protected virtual void OnRequest(HttpRequest request)
 {
     using (var response = request.Context.Response)
     {
         var apiHandle = ApiHandles.FirstOrDefault((item) => request.Url.AbsolutePath.StartsWith(item.Key, StringComparison.OrdinalIgnoreCase));
         if (!string.IsNullOrEmpty(apiHandle.Key))                                 //api处理器
         {
             response.AddHeader("Content-type", "application/json;charset=utf-8"); //添加响应头信息
             response.ContentEncoding = Encoding.UTF8;
             DotNet.Result result = new DotNet.Result()
             {
                 Message = "系统错误"
             };
             try
             {
                 result = OnBeginRequest(request, apiHandle);
             }
             catch (Exception ex)
             {
                 result = new DotNet.Result()
                 {
                     Message = "系统错误:" + ex.Message
                 };
             }
             finally
             {
                 using (System.IO.StreamWriter sw = new System.IO.StreamWriter(response.OutputStream))
                 {
                     sw.Write(result.ToJson());
                 }
             }
             response.Close();
             return;
         }
         var staticFile = StaticFiles.FirstOrDefault((item) => request.Url.AbsolutePath.StartsWith(item.Key, StringComparison.OrdinalIgnoreCase));
         if (!string.IsNullOrEmpty(staticFile.Value))//静态资源
         {
             var path    = staticFile.Value;
             var urlPath = request.Url.AbsolutePath.Remove(0, staticFile.Key.Length);
             if (!string.IsNullOrEmpty(urlPath))
             {
                 urlPath = urlPath.Remove(0, 1).Replace('/', System.IO.Path.DirectorySeparatorChar);
             }
             WriteStaticFile(request, urlPath, staticFile.Value);
             return;
         }
         else
         {
             WriteStaticFile(request, request.Url.AbsolutePath.Remove(0, 1), WorkPath);
             return;
         }
     }
 }