Skip to content

Commit 833fb6d

Browse files
author
Bart Koelman
committed
Simplified signature of app.UseJsonApi(), allowing for more flexibility in pipeline setup.
1 parent d5a87df commit 833fb6d

File tree

6 files changed

+26
-45
lines changed

6 files changed

+26
-45
lines changed

docs/getting-started/step-by-step.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ public void ConfigureServices(IServiceCollection services)
9696
}
9797
```
9898

99-
Add the middleware to the Startup.Configure method. Note that under the hood,
100-
this will call `app.UseRouting()` and `app.UseEndpoints(...)` so there is no need to add that as well.
99+
Add the middleware to the Startup.Configure method.
101100

102101
```c#
103102
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
104103
public void Configure(IApplicationBuilder app)
105104
{
105+
app.UseRouting();
106106
app.UseJsonApi();
107+
app.UseEndpoints(endpoints => endpoints.MapControllers());
107108
}
108109
```
109110

@@ -125,7 +126,9 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
125126
context.SaveChanges();
126127
}
127128

129+
app.UseRouting();
128130
app.UseJsonApi();
131+
app.UseEndpoints(endpoints => endpoints.MapControllers());
129132
}
130133
```
131134

@@ -135,4 +138,3 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
135138
dotnet run
136139
curl http://localhost:5000/people
137140
```
138-

docs/usage/extensibility/middleware.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Add the following to your Startup.ConfigureServices method. Replace AppDbContext
55
services.AddJsonApi<AppDbContext>();
66
```
77

8-
Add the middleware to the Startup.Configure method. Note that under the hood,
9-
this will call `app.UseRouting()` and `app.UseEndpoints(...)` so there is no need to add that as well.
8+
Add the middleware to the Startup.Configure method.
109

1110
```c3
11+
app.UseRouting();
1212
app.UseJsonApi();
13+
app.UseEndpoints(endpoints => endpoints.MapControllers());
1314
```

src/Examples/GettingStarted/Startup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace GettingStarted
99
{
1010
public sealed class Startup
1111
{
12-
// This method gets called by the runtime. Use this method to add services to the container.
12+
// This method gets called by the runtime. Use this method to add services to the container.
1313
public void ConfigureServices(IServiceCollection services)
1414
{
1515
services.AddDbContext<SampleDbContext>(
@@ -19,14 +19,16 @@ public void ConfigureServices(IServiceCollection services)
1919
options => options.Namespace = "api");
2020
}
2121

22-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
22+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2323
public void Configure(IApplicationBuilder app, SampleDbContext context)
2424
{
2525
context.Database.EnsureDeleted();
2626
context.Database.EnsureCreated();
2727
CreateSampleData(context);
2828

29+
app.UseRouting();
2930
app.UseJsonApi();
31+
app.UseEndpoints(endpoints => endpoints.MapControllers());
3032
}
3133

3234
private static void CreateSampleData(SampleDbContext context)

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public void Configure(
7373
AppDbContext context)
7474
{
7575
context.Database.EnsureCreated();
76+
77+
app.UseRouting();
7678
app.UseJsonApi();
79+
app.UseEndpoints(endpoints => endpoints.MapControllers());
7780
}
7881
}
7982
}

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
4343
{
4444
context.Database.EnsureCreated();
4545

46+
app.UseRouting();
4647
app.UseJsonApi();
48+
app.UseEndpoints(endpoints => endpoints.MapControllers());
4749
}
4850
}
4951
}
Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,26 @@
1-
using JsonApiDotNetCore.Internal;
21
using JsonApiDotNetCore.Middleware;
32
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.Extensions.DependencyInjection;
53

64
namespace JsonApiDotNetCore
75
{
86
public static class ApplicationBuilderExtensions
97
{
108
/// <summary>
11-
/// Validates the resource graph and optionally registers the JsonApiDotNetCore middleware.
9+
/// Registers the JsonApiDotNetCore middleware.
1210
/// </summary>
13-
/// <remarks>
14-
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer
15-
/// is responsible for registering required middleware.
16-
/// </remarks>
17-
/// <param name="app"></param>
18-
/// <param name="skipRegisterMiddleware">Indicates to not register any middleware. This enables callers to take full control of middleware registration order.</param>
19-
/// <param name="useAuthentication">Indicates if 'app.UseAuthentication()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
20-
/// <param name="useAuthorization">Indicates if 'app.UseAuthorization()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
11+
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
2112
/// <example>
22-
/// The next example illustrates how to manually register middleware.
23-
/// <code>
24-
/// app.UseJsonApi(skipRegisterMiddleware: true);
13+
/// The code below is the minimal that is required for proper activation,
14+
/// which should be added to your Startup.Configure method.
15+
/// <code><![CDATA[
2516
/// app.UseRouting();
26-
/// app.UseMiddleware{JsonApiMiddleware}();
17+
/// app.UseJsonApi();
2718
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
28-
/// </code>
19+
/// ]]></code>
2920
/// </example>
30-
public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false)
21+
public static void UseJsonApi(this IApplicationBuilder builder)
3122
{
32-
if (!skipRegisterMiddleware)
33-
{
34-
// An endpoint is selected and set on the HttpContext if a match is found
35-
app.UseRouting();
36-
37-
if (useAuthentication)
38-
{
39-
app.UseAuthentication();
40-
}
41-
42-
if (useAuthorization)
43-
{
44-
app.UseAuthorization();
45-
}
46-
47-
// middleware to run after routing occurs.
48-
app.UseMiddleware<JsonApiMiddleware>();
49-
50-
// Executes the endpoints that was selected by routing.
51-
app.UseEndpoints(endpoints => endpoints.MapControllers());
52-
}
23+
builder.UseMiddleware<JsonApiMiddleware>();
5324
}
5425
}
5526
}

0 commit comments

Comments
 (0)