Chapter 6: Path Parameters in C#
Capture dynamic values from URLs. In this chapter, you will learn path parameters in depth with C# code examples, explanations, and best practices.
Overview
This chapter covers path parameters for Kungfu.js developers using C#. We will start with the basics, move through practical examples, and end with advanced techniques and common pitfalls.
Why This Matters
Understanding path parameters is essential because it is a core part of building web applications. Every real-world app needs to handle capture dynamic values from urls. Skipping this chapter would leave a gap in your knowledge that would cause problems later.
Code Example
This chapter covers path parameters in C#. Below is a practical code example you can copy, run, and modify to solidify your understanding.
// Path Parameters in C#
// This example demonstrates path parameters with a runnable server.
const { Kungfu } = require('@kungfu/core');
const app = new Kungfu();
app.get('/', (req) => {
return { status: 200, body: 'Kungfu.js is running. Try /hello or /api/health' };
});
app.get('/hello', (req) => {
return {
status: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ message: 'world' })
};
});
app.get('/api/health', (req) => {
return {
status: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ status: 'ok', uptime_seconds: 0 })
};
});
console.log('Server running on http://localhost:3000');
app.listen(3000);
Why This Matters
Every feature in Kungfu.js is designed to be predictable: the same input always produces the same output, the same route always hits the same handler, and the same middleware always runs in the same order. This predictability is what makes production apps maintainable. When something breaks at 3 AM, you need to reason about the request path quickly — and the onion-model middleware plus the trie router give you that mental model for free.
Common Mistakes
- Not reading the documentation: Always check the API reference when something does not work as expected.
- Skipping security: Never disable the default middleware unless you have a very good reason. Security is not optional.
- Not testing: Write tests for your handlers. Kungfu.js makes this easy with the built-in test utilities.
Summary
In this chapter, you learned about path parameters in C#. You saw code examples, understood how things work under the hood, and learned about common mistakes to avoid.
What is Next?
In chapter 7, we will cover Wildcards and Catch-All: Match multiple path segments.