ASP.NET MVC Routing Module รับผิดชอบ Map Incoming Browser Requests ไปยัง MVC Controller Actions เฉพาะ เมื่อ ASP.NET MVC Application Launch แล้ว Application จะ Register Pattern หนึ่งหรือมากกว่ากับ Route Table ของ Framework เพื่อบอก Routing Engine ว่าจะทำอะไรกับ Request ที่ Match Patterns นั้น เมื่อ Routing Engine รับ Request Runtime จะ Match URL ของ Request ต่อ URL Patterns ที่ Registered และให้ Response ตาม Pattern Match
ASP.NET Introduce Routing เพื่อ Eliminate ความจำเป็นในการ Map แต่ละ URL กับ Physical File Routing ให้เรา Define URL Pattern ที่ Map ไปยัง Request Handler System.Web.Routing ถูกใช้โดย MVC Framework แต่ยังถูกใช้โดย ASP.NET Dynamic Data MVC Framework ใช้ Routing เพื่อ Direct Request ไปยัง Controller ไฟล์ Global.asax คือส่วนของ Application ที่เราจะ Define Route สำหรับ Application
Route Config ของ MVC Application
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
Route Name
Route คือ URL Pattern ที่ Map ไปยัง Handler Handler อาจเป็น Controller ใน MVC Application ที่ Process Request Route Name อาจใช้เป็น Reference เฉพาะสำหรับ Route ที่กำหนด
Constraints
Set ของ Constraints ที่ Apply ต่อ URL Pattern เพื่อ Define URL ที่ Match อย่างแคบลง
URL Pattern
URL Pattern สามารถมี Literal Values และ Variable Placeholders Literals และ Placeholders ตั้งอยู่ใน Segments ของ URL ที่ Delimit ด้วย Slash (/) Character
เมื่อ Request ถูกสร้าง URL จะ Parse เป็น Segments และ Placeholders และ Variable Values จะถูกส่งให้ Request Handler กระบวนการนี้คล้ายกับการ Parse Query Strings และส่งให้ Request Handler ในทั้งสองกรณี Variable Information ถูกรวมใน URL และส่งให้ Handler ในรูปแบบ Key-Value Pairs สำหรับ Query Strings Keys และ Values อยู่ใน URL สำหรับ Routes Keys คือ Placeholder Names ที่ Define ใน URL Pattern และ Values เท่านั้นที่อยู่ใน URL
Defaults
เมื่อเรา Define Route เราสามารถ Assign Default Value ให้ Parameter ได้ Defaults เป็น Object ที่มี Default Route Values สาม Segment ของ Default Route ประกอบด้วย Controller, Action และ Id

ใน URL ด้านบน Controller และ Action ที่ตรงกันจะถูก Match ถ้าเราไม่ส่ง Controller และ Action Method ใน URL และพึ่งพา Default Route Controller และ Action Method ที่ตรงกันจะถูก Invoke

ตัวอย่าง URL และค่าที่ได้
| URL | Controller | Action | Id |
|---|---|---|---|
| / | Home (Default) | Index (Default) | null (Optional) |
| /Home | Home | Index (Default) | null |
| /Home/Index | Home | Index | null |
| /Product/Detail/123 | Product | Detail | 123 |
| /Product/List | Product | List | null |
Route Constraints
// Constraint: id ต้องเป็นตัวเลข
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" } // Regex: ตัวเลขเท่านั้น
);
// Constraint: HttpMethod
routes.MapRoute(
name: "ApiRoute",
url: "api/{controller}/{action}/{id}",
constraints: new { httpMethod = new HttpMethodConstraint("GET", "POST") }
);
Attribute Routing (Alternative)
[RoutePrefix("api/products")]
public class ProductsController : ApiController {
[Route("{id:int}")] // GET api/products/123
[Route("")] // GET api/products
[Route("search/{query}")] // GET api/products/search/iphone
public IHttpActionResult Get(int id) { ... }
}