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

Implicitly execute matched endpoint at the end of middleware pipeline #1059

Merged
merged 4 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public RequestDelegate Build()
{
RequestDelegate app = context =>
{
// Implicitly execute matched endpoint at the end of the pipeline instead of returning 404
var endpointRequestDelegate = context.GetEndpoint()?.RequestDelegate;
if (endpointRequestDelegate != null)
{
return endpointRequestDelegate(context);
}

context.Response.StatusCode = 404;
return Task.CompletedTask;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit;

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

[Fact]
public void BuildImplicitlyCallsMatchedEndpointAsLastStep()
{
var builder = new ApplicationBuilder(null);
var app = builder.Build();

var endpointCalled = false;
var endpoint = new Endpoint(
context =>
{
endpointCalled = true;
return Task.CompletedTask;
},
EndpointMetadataCollection.Empty,
"Test endpoint");

var httpContext = new DefaultHttpContext();
httpContext.SetEndpoint(endpoint);

app.Invoke(httpContext);

Assert.True(endpointCalled);
}

[Fact]
public void BuildDoesNotCallMatchedEndpointWhenTerminated()
{
var builder = new ApplicationBuilder(null);
builder.Use((context, next) =>
{
// Do not call next
return Task.CompletedTask;
});
var app = builder.Build();

var endpointCalled = false;
var endpoint = new Endpoint(
context =>
{
endpointCalled = true;
return Task.CompletedTask;
},
EndpointMetadataCollection.Empty,
"Test endpoint");

var httpContext = new DefaultHttpContext();
httpContext.SetEndpoint(endpoint);

app.Invoke(httpContext);

Assert.False(endpointCalled);
}

[Fact]
public void PropertiesDictionaryIsDistinctAfterNew()
{
Expand Down