Skip to content

Glareone/Minimal-API-Controllerless-Fluent-Net8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Minimal API and best practices for .Net8 and .Net6

Table of content, projects with detailed examples

  1. Fundamentals. Lambdas. Flat Structure file
  2. Architecturing MinimalAPI. Reflection & Class registration
  3. CORS for MinimalAPI
  4. IOptions, Configuration, IOptionMonitor, IOptionSnapshot, IOptionFactory
  5. Exception Handling following IETF Standard and custom handling
  6. Logging
  7. DTO Fluent Validation. Enrich Swagger with Validation Rules Description
  8. Automapper
  9. Dapper & Automapper
  10. Authentication using JWT
  11. Authorization using different methods
  12. Localization & Globalization using resx. DateTime Converter
  13. Benchmakring ControllerAPI & MinimalAPI
  14. Error handling, RFC 9547 Modern standard and outdated RFC 7807

Minimal API .Net6. General Information

Examples:

image
image

CORS

Examples:

image
image

Run FrontendSPA to test CORS

In order to test CORS you may run Index.html file hosted under Course03 project in Frontend-for-CORS folder using LiveReloadServer

To Install LiveReloadServer for .Net6 and .Net7:

  1. cmd -> dotnet install -g --version 1.1.0 LiveReloadServer
  2. cmd -> dontnet install -g LiveReloadServer

To host Index.Html file in server's memory:

  1. cmd -> livereloadserver {BasePath}\Chapter03-CORS-GlobalAPISettings\Folder-For-Frontend

IOptions vs IOptionMonitor vs IOptionSnapshot

Examples:

image

Error handling using IETF Standard

RFC 9457 (July 2023, Modern) - must specify the details of each validation error

RFC 9457 Doc:

Media Type: application/problem+json

Format proposed by the standard:

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
Content-Language: en

{
 "type": "https://example.net/validation-error",
 "title": "Your request is not valid.",
 "errors": [
             {
               "detail": "must be a positive integer",
               "pointer": "#/age"
             },
             {
               "detail": "must be 'green', 'red' or 'blue'",
               "pointer": "#/profile/color"
             }
          ]
}

Implemented example with traceId and requestId:

{
  "type": "Bad Request",
  "title": "Something went wrong with OpenWeather Service",
  "status": 400,
  "detail": "Response status code does not indicate success: 401 (Unauthorized).",
  "instance": "GET /get-weather",
  "requestId": "0HN8TPG6GMH5R:00000007",
  "traceId": "00-352bd6ae37ee396964f8a999d6762418-ffde06642c1bdaf4-00"
}

Examples:

RFC 7807 (OBSOLETE)

RFC 7807 (OBSOLETE) And Documentation:

Media Type: application/problem+json Proposed Format:

 HTTP/1.1 403 Forbidden
   Content-Type: application/problem+json
   Content-Language: en

   {
    "type": "https://example.com/probs/out-of-credit",
    "title": "You do not have enough credit.",
    "detail": "Your current balance is 30, but that costs 50.",
    "instance": "/account/12345/msgs/abc",
    "balance": 30,
    "accounts": ["/account/12345",
                 "/account/67890"]
   }

Examples:

image

Configuring Logging in .Net6 MinimalAPI

Examples:

image
image

Fluent Validation

Examples:

image

Benchmarking using K6

http_req: 20078 - means a number of requests handled per second and total number of them
image

Compare Controllers vs MinimalAPI

image

The difference is approximately 5-20%.
image
results
minimalapi-text-plain
controllerapi-text-plain