NET Centric ComputingUnit 147 min read
URL Routing & Deployment in ASP.NET Core: Concepts, Techniques & Real-World Setup
Unit 14 of NET Centric Computing explores how ASP.NET Core maps URLs to controllers/actions (routing) and deploys web apps to servers (IIS, Linux, Docker). Covers route templates, attribute routing, deployment pipelines, and cloud hosting with hands-on examples from eSewa’s payment gateway and Daraz’s order system.
Core Concepts: What is URL Routing?
URL routing determines how incoming HTTP requests map to controllers and actions in ASP.NET Core. It converts a URL like /products/123 into a method call like ProductsController.GetProduct(123).
How Routing Works
- Route Template: A pattern (e.g.,
products/{id}) that matches URLs. - Route Data: Extracted values (e.g.,
id = 123) passed to the controller. - Route Constraints: Rules to validate route parameters (e.g.,
{id:int}ensuresidis numeric).
flowchart TD
A["HTTP Request\n(e.g., GET /orders/456)"] --> B["Route Matcher"]
B --> C["Route Template\n(orders/{id:int})"]
C --> D["Extract Parameters\n(id = 456)"]
D --> E["Invoke Controller\nOrdersController.GetOrder(456)"]
E --> F["Return Response\n(HTML/JSON)"]Types of Routing in ASP.NET Core
| Type | Definition | Example | Use Case |
|---|---|---|---|
| Conventional | Defined in Startup.cs via MapControllerRoute(). |
routes.MapRoute("default", "{controller}/{action}/{id}"); |
Legacy apps, simple URL structures. |
| Attribute | Uses [Route] or [HttpGet] attributes on controller actions. |
[Route("api/users/{id}")] |
REST APIs, microservices. |
| Attribute + Template | Combines attribute routing with template constraints. | [Route("products/{category}/{id:int}")] |
E-commerce filters (e.g., /electronics/123). |
Real-World Example: Daraz’s Order System
Daraz uses attribute routing to handle URLs like:
/orders/{orderId}/status→OrdersController.GetStatus(orderId)/products/{category}/search?q={query}→ProductsController.Search(category, query)
Why?
- Scalability: Route constraints (e.g.,
{category:alpha}) validate inputs before processing. - SEO-Friendly: Clean URLs (e.g.,
/electronics/laptops) rank better in search engines.
Deployment: Getting Your App Live
Deployment moves your ASP.NET Core app from development to a production server (IIS, Linux, Azure, etc.). Key steps:
1. Publishing the App
Use the dotnet publish command to create a deployable folder:
dotnet publish -c Release -o ./publish
Output Structure:
publish/
├── app.dll # Main app assembly
├── web.config # IIS configuration (if using Windows)
└── wwwroot/ # Static files (CSS, JS, images)
2. Deployment Targets
| Target | Platform | Tools/Commands | Example Use Case |
|---|---|---|---|
| IIS (Windows) | Windows Server | dotnet publish --output ./publish + IIS setup |
Corporate intranets (e.g., NTC’s internal tools). |
| Linux (Nginx) | Ubuntu/CentOS | dotnet publish -r linux-x64 + Nginx reverse proxy |
High-traffic sites (e.g., eSewa). |
| Docker | Any OS | docker build -t myapp . + docker run |
Cloud-native apps (e.g., Pathao’s backend). |
| Azure App Service | Cloud | az webapp up (Azure CLI) |
Startups needing auto-scaling. |
Step-by-Step: Deploying to IIS (Windows)
- Publish the App:
dotnet publish -c Release -o C:\inetpub\wwwroot\MyApp - Configure IIS:
- Open IIS Manager → Add Application Pool (target .NET CLR version).
- Set the Default Document to
index.html(orindex.cshtmlfor Razor).
- Test:
- Access
http://localhost/MyAppin a browser.
- Access
Worked Example: Ncell’s Billing Portal Ncell’s portal uses IIS deployment for:
- URL Routing:
/bills/{customerId}→BillsController.GetBill(customerId). - Static Files: CSS/JS served from
wwwrootfor fast loading. - Security: HTTPS enforced via IIS binding.
Advanced: Custom Route Constraints
Constraints validate route parameters. Common types:
{id:int}: Only integers (e.g.,/orders/123).{name:minlength(3)}: Strings ≥3 chars (e.g.,/users/john).{year:regex(^\d{4}$)}: 4-digit years (e.g.,/reports/2023).
Example: eSewa’s Payment Links
eSewa uses {transactionId:guid} to ensure only valid UUIDs (e.g., /payments/550e8400-e29b-41d4-a716-446655440000) are processed.
[Route("payments/{transactionId:guid}")]
public IActionResult ConfirmPayment(string transactionId) { ... }
Common Deployment Pitfalls & Fixes
| Issue | Cause | Solution |
|---|---|---|
| 500 Internal Server Error | Missing dependencies in production. | Use dotnet publish -c Release with all configs. |
| Mixed Content Warnings | HTTP/HTTPS mismatch for static files. | Force HTTPS in Startup.cs: app.UseHttpsRedirection(). |
| Slow Load Times | No caching for static files. | Enable static file caching in IIS/Nginx. |
| Route Not Found | Incorrect route template. | Check Startup.cs and attribute routes. |
In the Real World
eSewa’s Payment Gateway
- Routing: Uses
[Route("pay/{id:guid}")]to validate transaction IDs securely. - Deployment: Hosted on Linux (Nginx) for scalability during Diwali sales.
- Routing: Uses
Daraz’s Order Queue
- Routing:
/orders/{status}maps toOrdersController.FilterByStatus(status). - Deployment: Docker containers auto-scale during sales events.
- Routing:
NTC’s Service Portal
- Routing:
/bills/{customerId:int}ensures only numeric IDs are processed. - Deployment: IIS on Windows Server for internal team access.
- Routing:
Exam Tip
Define Key Terms:
- Route Template: Pattern matching URLs (e.g.,
products/{id}). - Attribute Routing:
[Route]attributes on controller actions. - Deployment: Process of publishing code to a live server (IIS/Linux/Docker).
- Route Template: Pattern matching URLs (e.g.,
Common Exam Questions:
- "Explain how
MapControllerRouteworks." → Cover route templates, defaults, and constraints. - "Deploy an ASP.NET Core app to IIS." → Steps: publish → IIS app pool → test.
- "What’s the difference between conventional and attribute routing?" → Use the comparison table above.
- "Explain how
Practical Tip:
- Always test routes locally with
dotnet runbefore deploying. - Use
dotnet publish -c Releaseto catch missing files early.
- Always test routes locally with
Visual Summary:
mindmap
root((URL Routing & Deployment))
Concepts
Route Template
Attribute Routing
Constraints
Deployment
IIS (Windows)
Linux (Nginx)
Docker
Azure
Real-World
eSewa (Linux/Nginx)
Daraz (Docker)
NTC (IIS)Based on the TU BSc CSIT syllabus for NET Centric Computing (CSC367), unit 14.
Discussion
Loading…