Skip to content

Commit f17a892

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Move API reporting to 'executing' rather than 'executed'. Fixes #273. Fixes #290.
1 parent 04a5485 commit f17a892

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Microsoft.AspNetCore.Mvc.Versioning/ReportApiVersionsAttribute.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public ReportApiVersionsAttribute( IReportApiVersions reportApiVersions )
3030
}
3131

3232
/// <summary>
33-
/// Reports the discovered service API versions for the given context after an action has executed.
33+
/// Reports the discovered service API versions for the given context before an action has executed.
3434
/// </summary>
35-
/// <param name="context">The <see cref="ActionExecutedContext">context</see> for the executed action.</param>
35+
/// <param name="context">The <see cref="ActionExecutingContext">context</see> for the executing action.</param>
3636
/// <remarks>This method will write the "api-supported-versions" and "api-deprecated-versions" HTTP headers into the
37-
/// response provided that there is a response and the executed action was not version-neutral.</remarks>
38-
public override void OnActionExecuted( ActionExecutedContext context )
37+
/// response provided the executing action is not version-neutral. This operation should be performed before the
38+
/// action is executed instead of after as HTTP headers cannot be specified after the response body has started
39+
/// streaming to the client.</remarks>
40+
public override void OnActionExecuting( ActionExecutingContext context )
3941
{
4042
var response = context.HttpContext.Response;
4143

test/Microsoft.AspNetCore.Mvc.Versioning.Tests/ReportApiVersionsAttributeTest.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@
66
using FluentAssertions;
77
using Http;
88
using Moq;
9+
using System.Collections.Generic;
910
using System.Linq;
1011
using Versioning;
1112
using Xunit;
1213

1314
public class ReportApiVersionsAttributeTest
1415
{
15-
static ActionExecutedContext CreateContext( ApiVersionModel model )
16+
static ActionExecutingContext CreateContext( ApiVersionModel model )
1617
{
1718
var headers = new HeaderDictionary();
1819
var response = new Mock<HttpResponse>();
1920
var httpContext = new Mock<HttpContext>();
2021
var action = new ActionDescriptor();
2122
var actionContext = new ActionContext( httpContext.Object, new RouteData(), action );
23+
var filters = new IFilterMetadata[0];
24+
var actionArguments = new Dictionary<string, object>();
25+
var controller = default( object );
2226

2327
response.SetupGet( r => r.Headers ).Returns( headers );
2428
httpContext.SetupGet( c => c.Response ).Returns( response.Object );
2529
action.SetProperty( model );
2630

27-
return new ActionExecutedContext( actionContext, new IFilterMetadata[0], null );
31+
return new ActionExecutingContext( actionContext, filters, actionArguments, controller );
2832
}
2933

3034
[Fact]
31-
public void on_action_executed_should_add_version_headers()
35+
public void on_action_executing_should_add_version_headers()
3236
{
3337
// arrange
3438
var supported = new[] { new ApiVersion( 1, 0 ), new ApiVersion( 2, 0 ) };
@@ -38,7 +42,7 @@ public void on_action_executed_should_add_version_headers()
3842
var attribute = new ReportApiVersionsAttribute();
3943

4044
// act
41-
attribute.OnActionExecuted( context );
45+
attribute.OnActionExecuting( context );
4246

4347
// assert
4448
context.HttpContext.Response.Headers["api-supported-versions"].Single().Should().Be( "1.0, 2.0" );
@@ -53,7 +57,7 @@ public void on_action_executing_should_not_add_headers_for_versionX2Dneutral_con
5357
var attribute = new ReportApiVersionsAttribute();
5458

5559
// act
56-
attribute.OnActionExecuted( context );
60+
attribute.OnActionExecuting( context );
5761

5862

5963
// assert

0 commit comments

Comments
 (0)