public void OnResultExecuting(ResultExecutingContext context)
 {
     var headerName = "OnResultExecuting";
     context.HttpContext.Response.Headers.Add(
         headerName, new string[] { "ResultExecutingSuccessfully" });
     _logger.LogInformation($"Header added: {headerName}");
 }
Ejemplo n.º 2
0
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            context.HttpContext.Response.Headers["Cache-Control"] = "no-cache, no-store, max-age=0";
            context.HttpContext.Response.Headers["Pragma"] = "no-cache";
            context.HttpContext.Response.Headers["Expires"] = "-1";

            base.OnResultExecuting(context);
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            filterContext.HttpContext.Response.Headers.Add("Pragma", "no-cache"); // HTTP 1.0.
            filterContext.HttpContext.Response.Headers.Add("Expires", "0"); // Proxies.

            base.OnResultExecuting(filterContext);
        }
Ejemplo n.º 4
0
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     if (context.ActionDescriptor.DisplayName == "FiltersWebSite.ProductsController.GetPrice")
     {
         context.HttpContext.Response.Headers.Append("filters",
             "On Action Result Filter - OnResultExecuting");
     }
 }
Ejemplo n.º 5
0
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.ActionDescriptor.DisplayName == "FiltersWebSite.ResultFilterController.GetHelloWorld")
            {
                context.Result = Helpers.GetContentResult(context.Result, "GlobalResultFilter.OnResultExecuting");
            }

            if (context.ActionDescriptor.DisplayName == "FiltersWebSite.ProductsController.GetPrice")
            {
                context.HttpContext.Response.Headers.Append("filters",
                    "Global Result Filter - OnResultExecuted");
            }
        }
Ejemplo n.º 6
0
        public virtual async Task OnResultExecutionAsync(
            ResultExecutingContext context,
            ResultExecutionDelegate next)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            OnResultExecuting(context);
            if (!context.Cancel)
            {
                OnResultExecuted(await next());
            }
        }
Ejemplo n.º 7
0
        public void FormatFilter_ContextContainsFormat_InRouteAndQueryData()
        {
            // If the format is present in both route and query data, the one in route data wins

            // Arrange
            var mediaType = MediaTypeHeaderValue.Parse("application/json");
            var mockObjects = new MockObjects("json", FormatSource.RouteData);
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(c => c.Response).Returns(new Mock<HttpResponse>().Object);

            // Query contains xml
            httpContext.Setup(c => c.Request.Query.ContainsKey("format")).Returns(true);
            httpContext.Setup(c => c.Request.Query["format"]).Returns("xml");

            // Routedata contains json
            var data = new RouteData();
            data.Values.Add("format", "json");

            var ac = new ActionContext(httpContext.Object, data, new ActionDescriptor());

            var resultExecutingContext = new ResultExecutingContext(
                ac,
                new IFilterMetadata[] { },
                new ObjectResult("Hello!"),
                controller: new object());

            var resourceExecutingContext = new ResourceExecutingContext(
                ac,
                new IFilterMetadata[] { });

            var filter = new FormatFilter(mockObjects.OptionsManager);

            // Act
            filter.OnResourceExecuting(resourceExecutingContext);
            filter.OnResultExecuting(resultExecutingContext);

            // Assert
            var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
            Assert.Equal(1, objectResult.ContentTypes.Count);
            AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]);
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public async Task OnResultExecutionAsync(
            ResultExecutingContext context,
            ResultExecutionDelegate next)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            var controller = context.Controller;
            if (controller == null)
            {
                throw new InvalidOperationException(Resources.FormatPropertyOfTypeCannotBeNull(
                    nameof(context.Controller),
                    nameof(ResultExecutingContext)));
            }

            IAsyncResultFilter asyncResultFilter;
            IResultFilter resultFilter;
            if ((asyncResultFilter = controller as IAsyncResultFilter) != null)
            {
                await asyncResultFilter.OnResultExecutionAsync(context, next);
            }
            else if ((resultFilter = controller as IResultFilter) != null)
            {
                resultFilter.OnResultExecuting(context);
                if (!context.Cancel)
                {
                    resultFilter.OnResultExecuted(await next());
                }
            }
            else
            {
                await next();
            }
        }
Ejemplo n.º 9
0
        public void FormatFilter_ExplicitContentType_SetOnResponse_TakesPrecedence()
        {
            // Arrange
            var mediaType = MediaTypeHeaderValue.Parse("application/foo");
            var mockObjects = new MockObjects("json", FormatSource.QueryData);
            var response = new Mock<HttpResponse>();
            response.Setup(r => r.ContentType).Returns("application/foo");
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(c => c.Response).Returns(response.Object);
            httpContext.Setup(c => c.Request.Query["format"]).Returns("json");
            var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
            var resultExecutingContext = new ResultExecutingContext(
                actionContext,
                new IFilterMetadata[] { },
                new ObjectResult("Hello!"),
                controller: new object());

            var resourceExecutingContext = new ResourceExecutingContext(
                actionContext,
                new IFilterMetadata[] { });

            var filter = new FormatFilter(mockObjects.OptionsManager);

            // Act
            filter.OnResourceExecuting(resourceExecutingContext);
            filter.OnResultExecuting(resultExecutingContext);

            // Assert
            var result = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
            Assert.Equal(0, result.ContentTypes.Count);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sets a Content Type on an  <see cref="ObjectResult" />  using a format value from the request.
        /// </summary>
        /// <param name="context">The <see cref="ResultExecutingContext"/>.</param>
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (!IsActive)
            {
                return; // no format specified by user, so the filter is muted
            }

            var objectResult = context.Result as ObjectResult;
            if (objectResult != null)
            {
                objectResult.ContentTypes.Clear();
                objectResult.ContentTypes.Add(ContentType);
            }
        }
Ejemplo n.º 11
0
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     context.Result = Helpers.GetContentResult(context.Result, "Result filter");
 }
Ejemplo n.º 12
0
 private static ResultExecutedContext CreateResultExecutedContext(ResultExecutingContext context)
 {
     return new ResultExecutedContext(context, context.Filters, context.Result, context.Controller);
 }
 public static void AfterOnResultExecuting(
     this DiagnosticSource diagnosticSource,
     ResultExecutingContext resultExecutingContext,
     IResultFilter filter)
 {
     if (diagnosticSource.IsEnabled("Microsoft.AspNet.Mvc.AfterOnResultExecuting"))
     {
         diagnosticSource.Write(
             "Microsoft.AspNet.Mvc.AfterOnResultExecuting",
             new
             {
                 actionDescriptor = resultExecutingContext.ActionDescriptor,
                 resultExecutingContext = resultExecutingContext,
                 filter = filter
             });
     }
 }
		//public override void OnActionExecuting(ActionExecutingContext context)
		//{
		//	_stopwatch = Stopwatch.StartNew();
		//}

		//public override void OnActionExecuted(ActionExecutedContext context)
		//{
		//	Debug.WriteLine($"Action ended in {_stopwatch.Elapsed}");
		//}

		public override void OnResultExecuting(ResultExecutingContext context)
		{
			Debug.WriteLine($"Result started after {_stopwatch.Elapsed}");
		}
Ejemplo n.º 15
0
        /// <summary>
        /// Sets a Content Type on an  <see cref="ObjectResult" />  using a format value from the request.
        /// </summary>
        /// <param name="context">The <see cref="ResultExecutingContext"/>.</param>
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var format = GetFormat(context);
            if (format == null)
            {
                // no format specified by user, so the filter is muted
                return;
            }

            var objectResult = context.Result as ObjectResult;
            if (objectResult == null)
            {
                return;
            }

            // If the action sets a single content type, then its takes precedence over the user
            // supplied content type based on format mapping.
            if ((objectResult.ContentTypes != null && objectResult.ContentTypes.Count == 1) ||
                !string.IsNullOrEmpty(context.HttpContext.Response.ContentType))
            {
                return;
            }

            var contentType = _options.FormatterMappings.GetMediaTypeMappingForFormat(format);
            objectResult.ContentTypes.Clear();
            objectResult.ContentTypes.Add(contentType);
        }
Ejemplo n.º 16
0
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     _logger.LogInformation("OnResultExecuting");
     base.OnResultExecuting(context);
 }
Ejemplo n.º 17
0
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     context.HttpContext.Response.Headers.Add(
         _name, new string[] { _value });
     base.OnResultExecuting(context);
 }
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     _logger.LogWarning("ClassFilter OnResultExecuting");
     base.OnResultExecuting(context);
 }
Ejemplo n.º 19
0
 public virtual void OnResultExecuting(ResultExecutingContext context)
 {
 }
Ejemplo n.º 20
0
 public virtual void OnResultExecuting(ResultExecutingContext context)
 {
 }
Ejemplo n.º 21
0
 public override void OnResultExecuting(ResultExecutingContext context)
 {
     throw new InvalidProgramException("Result Filter threw.");
 }
 public void OnResultExecuting(ResultExecutingContext context)
 {
     context.HttpContext.Response.Headers.Add(
         "Internal", new string[] { "Header Added" });
 }
Ejemplo n.º 23
0
 public virtual void OnResultExecuting([NotNull] ResultExecutingContext context)
 {
 }