Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 066c5ce

Browse files
authored
Implicitly execute matched endpoint at the end of middleware pipeline (#1059)
1 parent d77b370 commit 066c5ce

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public RequestDelegate Build()
8181
{
8282
RequestDelegate app = context =>
8383
{
84+
// Implicitly execute matched endpoint at the end of the pipeline instead of returning 404
85+
var endpointRequestDelegate = context.GetEndpoint()?.RequestDelegate;
86+
if (endpointRequestDelegate != null)
87+
{
88+
return endpointRequestDelegate(context);
89+
}
90+
8491
context.Response.StatusCode = 404;
8592
return Task.CompletedTask;
8693
};

test/Microsoft.AspNetCore.Http.Tests/Internal/ApplicationBuilderTests.cs

+55
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Http.Features;
57
using Xunit;
68

79
namespace Microsoft.AspNetCore.Builder.Internal
@@ -20,6 +22,59 @@ public void BuildReturnsCallableDelegate()
2022
Assert.Equal(404, httpContext.Response.StatusCode);
2123
}
2224

25+
[Fact]
26+
public void BuildImplicitlyCallsMatchedEndpointAsLastStep()
27+
{
28+
var builder = new ApplicationBuilder(null);
29+
var app = builder.Build();
30+
31+
var endpointCalled = false;
32+
var endpoint = new Endpoint(
33+
context =>
34+
{
35+
endpointCalled = true;
36+
return Task.CompletedTask;
37+
},
38+
EndpointMetadataCollection.Empty,
39+
"Test endpoint");
40+
41+
var httpContext = new DefaultHttpContext();
42+
httpContext.SetEndpoint(endpoint);
43+
44+
app.Invoke(httpContext);
45+
46+
Assert.True(endpointCalled);
47+
}
48+
49+
[Fact]
50+
public void BuildDoesNotCallMatchedEndpointWhenTerminated()
51+
{
52+
var builder = new ApplicationBuilder(null);
53+
builder.Use((context, next) =>
54+
{
55+
// Do not call next
56+
return Task.CompletedTask;
57+
});
58+
var app = builder.Build();
59+
60+
var endpointCalled = false;
61+
var endpoint = new Endpoint(
62+
context =>
63+
{
64+
endpointCalled = true;
65+
return Task.CompletedTask;
66+
},
67+
EndpointMetadataCollection.Empty,
68+
"Test endpoint");
69+
70+
var httpContext = new DefaultHttpContext();
71+
httpContext.SetEndpoint(endpoint);
72+
73+
app.Invoke(httpContext);
74+
75+
Assert.False(endpointCalled);
76+
}
77+
2378
[Fact]
2479
public void PropertiesDictionaryIsDistinctAfterNew()
2580
{

0 commit comments

Comments
 (0)