Skip to content

Commit 666875a

Browse files
Add optional params to control middleware (#665)
* Add optional param to add user middleware after routing * Add missing using * Rename to insertExtraMiddleware * Rework * docs: improved comments on usage UseJsonApi( ... ) Co-authored-by: Maurits Moeys <maurei@users.noreply.github.com>
1 parent efc1ab7 commit 666875a

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@ namespace JsonApiDotNetCore.Extensions
1515
public static class IApplicationBuilderExtensions
1616
{
1717
/// <summary>
18-
/// Adds necessary components such as routing to your application
18+
/// Runs several internal JsonApiDotNetCore services to ensure proper configuration and registers required middlewares.
19+
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer is
20+
/// is responsible for registering middleware that are required for JsonApiDotNetCore.
1921
/// </summary>
2022
/// <param name="app"></param>
21-
/// <returns></returns>
22-
public static void UseJsonApi(this IApplicationBuilder app)
23+
/// <param name="skipRegisterMiddleware">Indicates if JsonApiDotNetCore should skip middleware registration. This enabl.</param>
24+
/// <param name="useAuthentication">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
25+
/// <param name="useAuthorization">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
26+
/// <example>
27+
/// This example illustrate which required middlewares should be registered when using the <paramref name="skipRegisterMiddleware"/> option.
28+
/// <code>
29+
/// app.UseJsonApi(skipRegisterMiddleware: true);
30+
/// // JADNC requires routing
31+
/// app.UseRouting();
32+
/// // JADNC requires CurrentRequestMiddleware
33+
/// app.UseMiddleware<CurrentRequestMiddleware>();
34+
/// // JANDC requires the endpoint feature enabled as follows
35+
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
36+
/// </code>
37+
/// </example>
38+
public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false)
2339
{
2440
LogResourceGraphValidations(app);
2541
using (var scope = app.ApplicationServices.CreateScope())
@@ -28,14 +44,26 @@ public static void UseJsonApi(this IApplicationBuilder app)
2844
inverseRelationshipResolver?.Resolve();
2945
}
3046

31-
// An endpoint is selected and set on the HttpContext if a match is found
32-
app.UseRouting();
47+
if (!skipRegisterMiddleware) {
48+
// An endpoint is selected and set on the HttpContext if a match is found
49+
app.UseRouting();
3350

34-
// middleware to run after routing occurs.
35-
app.UseMiddleware<CurrentRequestMiddleware>();
51+
if (useAuthentication)
52+
{
53+
app.UseAuthentication();
54+
};
3655

37-
// Executes the endpoints that was selected by routing.
38-
app.UseEndpoints(endpoints => endpoints.MapControllers());
56+
if (useAuthorization)
57+
{
58+
app.UseAuthorization();
59+
};
60+
61+
// middleware to run after routing occurs.
62+
app.UseMiddleware<CurrentRequestMiddleware>();
63+
64+
// Executes the endpoints that was selected by routing.
65+
app.UseEndpoints(endpoints => endpoints.MapControllers());
66+
}
3967
}
4068

4169
/// <summary>

0 commit comments

Comments
 (0)