public void OnActionExecuted_HandlesExceptionAndReturnsObjectResult()
        {
            // Arrange
            var filter = new HttpResponseExceptionActionFilter();
            var httpContext = new DefaultHttpContext();
            httpContext.Request.Method = "GET";

            var actionContext = new ActionContext(
                                httpContext,
                                new RouteData(),
                                Mock.Of<ActionDescriptor>());

            var context = new ActionExecutedContext(
                actionContext,
                filters: new List<IFilterMetadata>(),
                controller: new object());

            context.Exception = new HttpResponseException(HttpStatusCode.BadRequest);

            // Act
            filter.OnActionExecuted(context);

            // Assert
            Assert.True(context.ExceptionHandled);
            var result = Assert.IsType<ObjectResult>(context.Result);
            Assert.Equal(typeof(HttpResponseMessage), result.DeclaredType);
            var response = Assert.IsType<HttpResponseMessage>(result.Value);
            Assert.NotNull(response.RequestMessage);
            Assert.Equal(context.HttpContext.GetHttpRequestMessage(), response.RequestMessage);
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpResponseException = context.Exception as HttpResponseException;
            if (httpResponseException != null)
            {
                var request = context.HttpContext.GetHttpRequestMessage();
                var response = httpResponseException.Response;

                if (response != null && response.RequestMessage == null)
                {
                    response.RequestMessage = request;
                }

                var objectResult = new ObjectResult(response)
                {
                    DeclaredType = typeof(HttpResponseMessage)
                };

                context.Result = objectResult;

                // Its marked as handled as in webapi because an HttpResponseException
                // was considered as a 'success' response.
                context.ExceptionHandled = true;
            }
        }
 public void OnActionExecuted(ActionExecutedContext context)
 {
     if (context.ActionDescriptor.DisplayName == "FiltersSample.Controllers.HomeController.Hello")
     {
         // Manipulating action result...
         context.Result = Helpers.GetContentResult(context.Result, "FIRST: ");
     }
 }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception != null)
            {
                context.Result = Helpers.GetContentResult(null, "Hi from Action Filter");

                context.Exception = null;
            }
        }
 public void OnActionExecuted(ActionExecutedContext filterContext)
 {
     if (filterContext.Exception == null)
     {
         //  var transaction = (ITransaction)filterContext.HttpContext.Items["ThisTransaction"];
         //  session.Flush();
         //  transaction.Commit();
     }
 }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Exception != null || (_verifyModelStateError && filterContext.ModelState.ErrorCount > 0))
            {
                _request.Finish(true); 
                return;
            }

            _request.Finish();
        }
Esempio n. 7
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     if (context.Exception != null)
     {
         // Rollback
     }
     else
     {
         _context.SaveChangesAsync();
     }
 }
Esempio n. 8
0
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.HttpContext.Response.StatusCode == 200 &&
                !actionExecutedContext.HttpContext.Response.Headers.ContainsKey("CacheControl"))
            {
                actionExecutedContext.HttpContext.Response.Headers["CacheControl"] = "no-cache";
                actionExecutedContext.HttpContext.Response.Headers["Pragma"] = "no-cache";

                if (!actionExecutedContext.HttpContext.Response.Headers.ContainsKey("Expires"))
                {
                    actionExecutedContext.HttpContext.Response.Headers["Expires"] = DateTimeOffset.UtcNow.AddDays(-1).ToString();
                }
            }
        }
            /// <summary>
            /// Adds the expiry headers
            /// </summary>
            /// <param name="context"></param>
            public void OnActionExecuted(ActionExecutedContext context)
            {
                //get the model from the items
                if (!context.HttpContext.Items.ContainsKey(nameof(AddExpiryHeadersAttribute))) return;
                var file = context.HttpContext.Items[nameof(AddExpiryHeadersAttribute)] as RequestModel;
                if (file == null) return;

                var etag = _hasher.Hash(file.FileKey + file.Compression + file.Mime);

                context.HttpContext.Response.AddETagResponseHeader(etag);
                context.HttpContext.Response.AddCacheControlResponseHeader();
                context.HttpContext.Response.AddLastModifiedResponseHeader(file);
                context.HttpContext.Response.AddExpiresResponseHeader();
            }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception != null)
            {
                var aEx = context.Exception as AggregateException;

                if (aEx != null)
                    HandleAggregateException(aEx);
                else
                    HandleException(context.Exception);

                context.ExceptionHandled = true;
            }

            base.OnActionExecuted(context);
        }
Esempio n. 11
0
 public virtual void OnActionExecuted(ActionExecutedContext context)
 {
 }
 /// <summary>
 /// Adds the compression headers
 /// </summary>
 /// <param name="context"></param>
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     context.HttpContext.Response.AddCompressionResponseHeader(context.HttpContext.Request.GetClientCompression());
 }
 public static void AfterOnActionExecuted(
     this DiagnosticSource diagnosticSource,
     ActionDescriptor actionDescriptor,
     ActionExecutedContext actionExecutedContext,
     IActionFilter filter)
 {
     if (diagnosticSource.IsEnabled("Microsoft.AspNet.Mvc.AfterOnActionExecuted"))
     {
         diagnosticSource.Write(
             "Microsoft.AspNet.Mvc.AfterOnActionExecuted",
             new
             {
                 actionDescriptor = actionDescriptor,
                 actionExecutedContext = actionExecutedContext,
                 filter = filter
             });
     }
 }
Esempio n. 14
0
        public override void OnActionExecuted(ActionExecutedContext context)
        { 

            if (IsEnabled)
            {
                //当 Action 有修改且正常执行完成后,保存事务;当 ReadOnly = true 时 或 有异常发生时,回滚事务
                bool writeChanges = HasChanges && context.Exception == null;

                try
                {
                    if (this.Session != null)
                    {
                        if (writeChanges)
                        {
                            this.Session.SaveChanges();
                        }
                        this.Callback.BeforeExit(this.Session);
                    }
                    if (this.TransactionScope != null)
                    {
                        if (writeChanges)
                        {
                            this.TransactionScope.Complete();
                        }

                        this.Callback.BeforeExit(this.TransactionScope);
                    }
                }
                finally
                {
                    if (this.TransactionScope != null)
                    {
                        this.TransactionScope.Dispose();
                    }
                }
            }

            base.OnActionExecuted(context);
        }
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     _logger.LogWarning("ClassFilter OnActionExecuted");
     base.OnActionExecuted(context);
 }
Esempio n. 16
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     context.HttpContext.Response.Headers.Append("filters", "On Action Action Filter - OnActionExecuted");
 }
Esempio n. 17
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     _logger.LogInformation("OnActionExecuted");
     base.OnActionExecuted(context);
 }