Refactor DIExtensions and add exception handling middleware
- Improved documentation in DIExtensions.cs for clarity. - Added project reference to DigitalData.Core.Exceptions. - Updated solution file to include DigitalData.Core.Exceptions. - Introduced ExceptionHandlingMiddleware for global exception handling. - Added BadRequestException and NotFoundException classes. - Created DigitalData.Core.Exceptions project with .NET 7.0, 8.0, and 9.0 support.
This commit is contained in:
@@ -1,53 +1,50 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using System.Configuration;
|
||||
namespace DigitalData.Core.API;
|
||||
|
||||
namespace DigitalData.Core.API
|
||||
/// <summary>
|
||||
/// Provides extension methods for adding middleware to the application's request pipeline.
|
||||
/// </summary>
|
||||
public static class DIExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for adding middleware to the application's request pipeline.
|
||||
/// Adds the <see cref="CSPMiddleware"/> to the application's request pipeline to include
|
||||
/// Content Security Policy (CSP) headers in the HTTP response.
|
||||
/// </summary>
|
||||
public static class DIExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the <see cref="CSPMiddleware"/> to the application's request pipeline to include
|
||||
/// Content Security Policy (CSP) headers in the HTTP response.
|
||||
/// </summary>
|
||||
/// <param name="app">The application builder.</param>
|
||||
/// <param name="policy">
|
||||
/// The CSP policy string with placeholders. The first format parameter {0} will be replaced
|
||||
/// by the nonce value.
|
||||
/// </param>
|
||||
/// <returns>The application builder with the CSP middleware added.</returns>
|
||||
public static IApplicationBuilder UseCSPMiddleware(this IApplicationBuilder app, string policy)
|
||||
=> app.UseMiddleware<CSPMiddleware>(policy);
|
||||
/// <param name="app">The application builder.</param>
|
||||
/// <param name="policy">
|
||||
/// The CSP policy string with placeholders. The first format parameter {0} will be replaced
|
||||
/// by the nonce value.
|
||||
/// </param>
|
||||
/// <returns>The application builder with the CSP middleware added.</returns>
|
||||
public static IApplicationBuilder UseCSPMiddleware(this IApplicationBuilder app, string policy)
|
||||
=> app.UseMiddleware<CSPMiddleware>(policy);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the DiP (Development in Production) mode is enabled for the WebApplicationBuilder.
|
||||
/// </summary>
|
||||
/// <param name="builder">The WebApplicationBuilder instance.</param>
|
||||
/// <returns>True if DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDiP(this WebApplicationBuilder builder) => builder.Configuration.GetValue<bool>("DiPMode");
|
||||
/// <summary>
|
||||
/// Checks if the DiP (Development in Production) mode is enabled for the WebApplicationBuilder.
|
||||
/// </summary>
|
||||
/// <param name="builder">The WebApplicationBuilder instance.</param>
|
||||
/// <returns>True if DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDiP(this WebApplicationBuilder builder) => builder.Configuration.GetValue<bool>("DiPMode");
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the DiP (Development in Production) mode is enabled for the WebApplication.
|
||||
/// </summary>
|
||||
/// <param name="app">The WebApplication instance.</param>
|
||||
/// <returns>True if DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDiP(this WebApplication app) => app.Configuration.GetValue<bool>("DiPMode");
|
||||
/// <summary>
|
||||
/// Checks if the DiP (Development in Production) mode is enabled for the WebApplication.
|
||||
/// </summary>
|
||||
/// <param name="app">The WebApplication instance.</param>
|
||||
/// <returns>True if DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDiP(this WebApplication app) => app.Configuration.GetValue<bool>("DiPMode");
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplicationBuilder.
|
||||
/// </summary>
|
||||
/// <param name="builder">The WebApplicationBuilder instance.</param>
|
||||
/// <returns>True if the environment is Development or DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDevOrDiP(this WebApplicationBuilder builder) => builder.Environment.IsDevelopment() || builder.IsDiP();
|
||||
/// <summary>
|
||||
/// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplicationBuilder.
|
||||
/// </summary>
|
||||
/// <param name="builder">The WebApplicationBuilder instance.</param>
|
||||
/// <returns>True if the environment is Development or DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDevOrDiP(this WebApplicationBuilder builder) => builder.Environment.IsDevelopment() || builder.IsDiP();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplication.
|
||||
/// </summary>
|
||||
/// <param name="app">The WebApplication instance.</param>
|
||||
/// <returns>True if the environment is Development or DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDevOrDiP(this WebApplication app) => app.Environment.IsDevelopment() || app.IsDiP();
|
||||
/// <summary>
|
||||
/// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplication.
|
||||
/// </summary>
|
||||
/// <param name="app">The WebApplication instance.</param>
|
||||
/// <returns>True if the environment is Development or DiP mode is enabled; otherwise, false.</returns>
|
||||
public static bool IsDevOrDiP(this WebApplication app) => app.Environment.IsDevelopment() || app.IsDiP();
|
||||
|
||||
/// <summary>
|
||||
/// Configures the services with options from the specified section of the appsettings.json file.
|
||||
@@ -65,5 +62,4 @@ namespace DigitalData.Core.API
|
||||
builder.Services.Configure<T>(section);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user