Skip to content

Request Validation in Minimal APIs is a .NET 9 application that demonstrates the use of minimal APIs in ASP.NET Core.

License

Notifications You must be signed in to change notification settings

JitenShahani/RequestValidationInMinimalAPIs

Repository files navigation

Request Validation in Minimal APIs Target Language Language

Request Validation in Minimal APIs is a .NET 9 application that demonstrates the use of minimal APIs in ASP.NET Core. This project includes various features such as global exception handling, CORS, HTTPS redirection, health checks, OpenAPI documentation, and more. It also showcases the use of middleware for handling exceptions and anti-forgery tokens, as well as the integration of Swagger UI with custom styles and dark mode support.

FluentValidation FluentValidation.DependencyInjection OpenApi HybridCaching HealthChecks SwaggerUI Serilog Serilog.Extensions.Logging Serilog.Sinks.File

Table of Contents

Features

  • Global Exception Handling: Middleware to handle unhandled exceptions and return a standardized error response.
  • Static Assets: Serve static assets like CSS, JS, and images.
  • Status Code Pages: Provide better error responses with status code pages.
  • CORS: Enable Cross-Origin Resource Sharing (CORS) for secure API access.
  • HTTPS Redirection: Redirect HTTP requests to HTTPS in non-development environments.
  • Health Checks: Implement health checks to monitor the application's health status.
  • OpenAPI Documentation: Generate OpenAPI documentation for API endpoints.
  • Swagger UI: Integrate Swagger UI with custom styles and dark mode support.
  • Rate Limiting: Enable rate limiting to control the number of requests.
  • Anti-Forgery Tokens: Use anti-forgery tokens to protect against CSRF attacks.
  • Hybrid Cache: Use Hybrid Cache to cache data and improve performance.
  • Serilog: Integrate Serilog for structured logging and better log management.
  • Minimal APIs: Demonstrate the use of minimal APIs for creating HTTP endpoints.

Blog Post Endpoints

Get all blog posts

  • Endpoint: GET /posts
  • Description: This endpoint returns all the blog posts from the database. The data will be cached using Hybrid Cache, and the response header X-Data-Source will indicate whether the data is coming from the Database or Cache.
  • Responses:
    • 200 OK: Returns a list of blog posts.
    • 204 No Content: No blog posts found.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Get a blog post by Id

  • Endpoint: GET /posts/{id:Guid}
  • Description: This endpoint returns a single blog post based on the blog post Id you provide. The data will be cached using Hybrid Cache, and the response header X-Data-Source will indicate whether the data is coming from the Database or Cache.
  • Responses:
    • 200 OK: Returns the blog post.
    • 404 Not Found: Blog post not found.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Create a new blog post

  • Endpoint: POST /posts
  • Description: This endpoint creates a new blog post based on the values you provide. The cache will be invalidated to ensure the new post is reflected in subsequent requests.
  • Request Body:
    • application/json: A JSON object containing the title and content of the blog post.
  • Responses:
    • 201 Created: Returns the Id of the created blog post.
    • 400 Bad Request: Invalid request data or missing anti-forgery token.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Update an existing blog post

  • Endpoint: PUT /posts
  • Description: This endpoint updates an existing blog post based on the values you provide. The cache will be invalidated to ensure the updated post is reflected in subsequent requests.
  • Request Body:
    • application/json: A JSON object containing the Id, title, and content of the blog post.
  • Responses:
    • 200 OK: Blog post updated successfully.
    • 404 Not Found: Blog post not found.
    • 400 Bad Request: Invalid request data or missing anti-forgery token.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Delete an existing blog post

  • Endpoint: DELETE /posts/{id:Guid}
  • Description: This endpoint deletes an existing blog post based on the blog post Id you provide. The cache will be invalidated to ensure the deleted post is not reflected in subsequent requests.
  • Responses:
    • 200 OK: Blog post deleted successfully.
    • 404 Not Found: Blog post not found.
    • 400 Bad Request: Missing anti-forgery token.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Health Check Endpoint

Get the health status of the application and the database

  • Endpoint: GET /health
  • Description: This endpoint returns the health status of the application along with it's database.
  • Responses:
    • 200 OK: Returns the health status of the application.
    • 204 No Content: The named HttpClient service is missing, the endpoint response is not 200 OK, or the health report is null.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Exception Endpoint

Trigger an unhandled exception

  • Endpoint: GET /exception
  • Description: This endpoint triggers an unhandled exception to demonstrate global exception handling.
  • Responses:
    • 200 OK: Returns the argument value if the Name parameter is not null.
    • 500 Internal Server Error: Returns a standardized error response with details of the exception. All unhandled exception(s) are caught by the middleware.

Anti-Forgery Token Endpoint

Get Anti-Forgery Token

  • Endpoint: GET /AFT
  • Description: This endpoint returns the Anti-Forgery Token. The token is required for POST, PUT, and DELETE requests to prevent Cross-Site Request Forgery (CSRF) attacks. This endpoint will not be visible in the Swagger UI.
  • Responses:
    • 200 OK: Returns the Anti-Forgery Token.
    • 500 Internal Server Error: Exception occurs. All unhandled exception(s) are caught by the middleware.

Getting Started

Prerequisites

Installation

  1. Clone the repository:
	  git clone https://github.com/JitenShahani/RequestValidationInMinimalAPIs.git
	  cd RequestValidationInMinimalAPIs
  1. Build the project:
	dotnet build
  1. Run the application:
	dotnet run --project .\RequestValidationInMinimalAPIs\RequestValidationInMinimalAPIs.csproj

or, use the watch command to automatically restart the application when changes are detected:

	dotnet watch --project .\RequestValidationInMinimalAPIs\RequestValidationInMinimalAPIs.csproj
  1. Open your browser and navigate to https://localhost:7036/swagger/index.html to view the Swagger UI.

Configuration

Ensure your appsettings.json file contains the required configuration settings for CORS origins, cache keys, and anti-forgery tokens.

Examples

Global Exception Handling

To see global exception handling in action, you can trigger an unhandled exception by accessing /exception endpoint that throws an ArgumentNull exception. The middleware will catch the exception and return a standardized error response.

CORS

To test CORS, try making a request to the API from a different origin. The CORS policy will allow or deny the request based on the configured origins.

HTTPS Redirection

To test HTTPS redirection, try accessing the application using HTTP. The application will automatically redirect the request to HTTPS provided the environment is not development.

Health Checks

To check the health status of the application, navigate to the /health endpoint. The application will return the health status of various components.

OpenAPI Documentation

To view the OpenAPI documentation, navigate to the /swagger endpoint. The Swagger UI will display the API documentation with custom styles and dark mode support.

Rate Limiting

To test rate limiting, make multiple requests to the API within a short period. The rate limiter will enforce the configured limits and return a 429 Too Many Requests response if the limit is exceeded.

Anti-Forgery Tokens

To test anti-forgery tokens, try making a POST, PUT, or DELETE request without including the anti-forgery token in the header. The request will be rejected to protect against Cross-Site Request Forgery (CSRF) attacks.

Hybrid Cache

To see Hybrid Cache in action, navigate to the /posts or /posts/{id:Guid} endpoints. The data will be cached for 5 minutes, and the response header X-Data-Source will indicate whether the data is coming from the Database or Cache.

Minimal APIs

To see minimal APIs in action, navigate to the various endpoints defined in the application. The minimal APIs provide a simple and efficient way to create HTTP endpoints.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contact

For questions or support, please open an issue on the GitHub repository.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss any changes or improvements.

Thanks to Contributors

About

Request Validation in Minimal APIs is a .NET 9 application that demonstrates the use of minimal APIs in ASP.NET Core.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •