Let's learn how a .NET Core API handles a user request, and how middlewares work.
The video follows one request from Kestrel all the way to the database and back.
If you remember one thing, make it this: middlewares run in the exact order you register them, and the response travels back through them in reverse.
That order once cost me almost 4 hours in production. UseAuthorization sat above UseAuthentication, so a perfectly valid token got a 401, with no exception and nothing in the logs.
This is my first time teaching a concept as a short animated video instead of an article.
Did this style work for you? And which .NET concept should I animate next?
Follow me for more such content.
One of the coolest .NET features nobody talks about:
-> Channels
You can find them in the System.Threading.Channels namespace.
Channels let you build async messaging with simple C# code.
They expose two APIs:
- One for writing messages to the channel
- One for reading messages (asynchronously) from the channel
You can run a worker in the background that picks up messages off the channel and sends them off for processing.
I've used this to build a primitive in-memory message bus (with all the drawbacks that come with it).
Here's how you could implement this: https://t.co/lsOm4iHH6b
Are you using Channels in your .NET applications?
.NET lets you add API rate limiting in one line of code.
It's excellent for controlling usage of your application.
It has some good and not-so-good aspects.
Rate limiting can make your APIs more robust and secure.
If you want to be a better software engineer, master background jobs.
Here's why they let you build more scalable systems.
Say I want to move some work in my application to the background.
I want this background work to run outside the normal application flow.
Here are a few examples:
- Reminding users that their subscription will renew
- Sending an email after completing a sale
- Processing messages from a queue
- Clearing stale cache items
You can set up a background task in .NET within minutes.
The most straightforward approach is to use the BackgroundService class.
With .NET 8, we can also start these background services concurrently.
If I need a more robust solution: Quartz is my first choice.
Here's why: https://t.co/sZ0uKDbYVO
---
P.S. I built this AI programming pack for Clean Architecture to make your life easier. More than 10,000+ developers are already using it. Get it here (it's free): https://t.co/34o1NguaZ6
You hear a lot of talk about coding with AI.
But it's surprisingly easy to use AI in .NET to build cool features.
I'm thinking about all the possibilities this unlocks...
A few things I tried:
- Simple chat + reply (vanilla)
- Summarization (practical)
- Categorization (now we're talking)
- Image recognition (this is fun to build)
Building this by running an LLM locally is mind-blowing.
And we're still getting started.
Learn more about running LLMs in .NET: https://t.co/ezBEXnTFjk
---
P.S. I built this AI programming pack for Clean Architecture to make your life easier. More than 10,000+ developers are already using it. Get it here (it's free): https://t.co/34o1NguaZ6
Full Claude Course
Master Claude to automate repetitive tasks, build apps without coding, create real portfolio projects, and become more valuable at work.
Do you know all 3 ways to define Middleware in ASP .NET Core?
Middleware allows you to introduce additional logic before or after executing an HTTP request. It's an excellent choice for solving cross-cutting concerns in your application.
You are already using many of the built-in middleware available in the framework.
But I'm going to show you three approaches to define custom middleware:
- Request Delegates
- Convention based
- Factory based
REST API Security Best Practices in ASP .NET Core
Every developer should know and teach AI agents ๐
Over the years, I have shipped and reviewed many .NET APIs, and the same set of practices keeps them safe.
In my latest newsletter article, I cover 18 security best practices with code examples: https://t.co/tCXdMbiUfw
That's the full list:
- Enforce HTTPS everywhere
- Authenticate with tokens, not sessions
- Validate the JWT signature, issuer, audience, and lifetime
- Authorize with policies, not just [Authorize]
- Apply the principle of least privilege
- Validate and sanitize all input
- Protect against over-posting / mass assignment
- Validate content types and limit request size
- Use parameterized queries and EF Core
- Implement rate limiting and throttling
- Configure CORS restrictively
- Return minimal error detail
- Set security headers
- Store secrets securely
- Enforce CSRF protection where relevant
- Version your API and deprecate insecure endpoints
- Log and audit security events
- Keep dependencies patched
Copy my article and save it as "dotnet-security-review-skill" for your AI agents. And run your codebase for a review on existing application.
You will be surprised how many issues it will find.
If you want to master ASP .NET Core and reach a Senior or Tech Lead level, here is your playbook: https://t.co/tKKl4rDdE5
๐๐ฒ๐๐ ๐๐ผ๐ผ๐น๐ ๐ผ๐ณ ๐๐ต๐ฒ ๐๐ฒ๐ฒ๐ธ:
How I Taught an AI to Sound Like Me: https://t.co/EsAHcw7hUp
AI agents are ideal for tackling well-scoped tasks so you can focus on larger projects, but they can also slow down your workflow. Most run on vendor servers, which means specialized software, API keys, and agent harnesses. Security and governance are outside your control. Here is how to fix: https://t.co/hZaIPmarge
โโ
โป๏ธ Repost to help others build more secure ASP .NET Core apps
โ Follow me ( @AntonMartyniuk ) to improve your .NET and Architecture Skills
Your applications need validation.
You can reinvent the wheel here, or you can use FluentValidation.
FluentValidation uses fluent syntax and lambda expressions to build strongly typed validation rules.
You can write rules ensuring properties are:
- Not null
- Not empty
- Have a min/max length
- Are equal/not equal to some value
- Define your completely custom rule
It's easy to write asynchronous validation rules using the MustAsync method. Dependency injection also works as you'd expect.
URL STRUCTURE IN API DESIGN
1. WHAT IS AN API URL?
An API URL identifies a resource that clients can access.
โ Every resource has a unique URL
โ URLs act as resource identifiers
โ Clients interact with resources through URLs
โ Well-designed URLs improve API usability
Clean URLs make APIs easier to understand and maintain.
2. USE NOUNS, NOT VERBS
URLs should represent resources rather than actions.
Good Examples:
โ /users
โ /products
โ /orders
Avoid:
โ /getUsers
โ /createProduct
โ /deleteOrder
HTTP methods already define the action being performed.
3. USE PLURAL RESOURCE NAMES
Collections should use plural nouns.
Good Examples:
โ /users
โ /products
โ /orders
Avoid:
โ /user
โ /product
โ /order
Plural names make API endpoints consistent.
4. USE HIERARCHICAL URLS
Nested resources should reflect relationships.
Examples:
โ /users/123/orders
โ /orders/456/items
โ /products/20/reviews
Hierarchical URLs clearly show resource ownership.
5. USE PATH PARAMETERS
Path parameters identify specific resources.
Examples:
โ /users/123
โ /products/45
โ /orders/789
Path parameters uniquely identify individual resources.
6. USE QUERY PARAMETERS FOR FILTERING
Query parameters modify or filter returned data.
Examples:
โ /users?page=2
โ /products?category=laptops
โ /orders?status=completed
โ /users?sort=name
Query parameters should not identify resources.
7. KEEP URLS SHORT AND READABLE
URLs should be simple and meaningful.
Good Examples:
โ /users/123
โ /orders/456/items
Avoid:
โ /application/get_all_registered_users_information
Short URLs are easier to remember and maintain.
8. USE LOWERCASE LETTERS
Use lowercase characters throughout URLs.
Good Examples:
โ /users
โ /products/15
Avoid:
โ /Users
โ /Products
Lowercase URLs prevent confusion and improve consistency.
9. USE HYPHENS FOR MULTIPLE WORDS
Separate multiple words with hyphens.
Good Examples:
โ /user-profiles
โ /customer-orders
โ /payment-methods
Avoid:
โ /user_profiles
โ /userProfiles
โ /userprofiles
Hyphens improve readability.
10. INCLUDE API VERSIONING
Version APIs to support future changes.
Examples:
โ /v1/users
โ /v2/products
โ /api/v1/orders
Versioning prevents breaking existing client applications.
11. AVOID FILE EXTENSIONS
Do not include file extensions in API URLs.
Good Examples:
โ /users
โ /products
Avoid:
โ /users.json
โ /products.xml
The client specifies the response format using HTTP headers.
12. DESIGN CONSISTENT URLS
Consistency makes APIs predictable.
โ Use the same naming conventions everywhere
โ Follow the same nesting style
โ Keep endpoints logically organized
โ Avoid unnecessary complexity
Consistent URL design improves developer productivity and API maintainability.
BENEFITS OF GOOD URL DESIGN
โ Easier to understand
โ Better developer experience
โ More maintainable APIs
โ Improves consistency across services
โ Supports RESTful best practices
Get the complete API Design ebook:
https://t.co/t2KOeauy6O
Do you want to improve your knowledge of software development?
Learn design patterns from these lessons I created over the past few years. ๐
26 ๐๐๐ญ๐ญ๐๐ซ๐ง๐ฌ:
โข Factory Method: https://t.co/jpTx3NZo6t
โข Abstract Factory: https://t.co/RdnCbGgsUN
โข Facade: https://t.co/k797kCSQnh
โข Chain of Responsibility: https://t.co/rj4bttRxba
โข Adapter: https://t.co/9iSc5VWFPW
โข CQRS: https://t.co/EPTHwW6g6t
โข Bridge: https://t.co/qVSzkNf7nV
โข Command: https://t.co/T5LCum4ves
โข Observer: https://t.co/z3PvbTX404
โข Decorator: https://t.co/UfKXOUJ8Ma
โข Singleton (multithreading): https://t.co/hIwIOXpfD2
โข Options (IOptions): https://t.co/sPelJzpL5I
โข Builder: https://t.co/V9Oyz9rmkf
โข State: https://t.co/SAQcYna3xJ
โข Memento: https://t.co/dBuFSWhBYc
โข Strategy: https://t.co/OrcWb5RedJ
โข Template Method: https://t.co/fvQyKMNfu4
โข Composite: https://t.co/ThVl03kKV8
โข Visitor: https://t.co/lShGtOv8sR
โข Proxy: https://t.co/Aqe6EIxQMH
โข Prototype: https://t.co/99gUO4aihd
โข REPR: https://t.co/Bcv0fUBPMb
โข Service Collection Extension: https://t.co/1EtKECNj0B
โข Saga Orchestration: https://t.co/3u9I7oeuqp
โข Outbox: https://t.co/C5hv1pssu3
โข Circuit Breaker (resilient APIs): https://t.co/nKSi3UZRIn
You can also check my 2 ebooks on Design Patterns:
https://t.co/5QQCOJTRxu
https://t.co/MP1XK414jF (Here's a 20% discount code: DEEP20 )
__
โป๏ธ Repost to help others learn more.
๐ Save this post for later - you'll thank yourself when refactoring at 2 AM.
โ Follow me (Stefan ฤokiฤ) to learn .NET and Architecture.
@TsMeSalman yaar! ek kaam karo, tum log jao, cheating krke jeet jao, hume koi atiaraz nahi hai, but bhai jo game accha khele usko support karo bura na maano.