Part2 -> Section1 The Basic -> 004. Routing

     โดยปกติ url ของเว็บไซต์โดยทั่วไปจะชี้ไปที่ชื่อไฟล์ เช่น home.aspx, home.php หรือ home.html แต่สำหรับ MVC นั้น url จะชี้ไปที่ Controller class, Action method(คือ method ใน Controller class นั่นเอง) และ parameter value ซึ่งจะเป็น pattern ดังนี้ {domain name}/{controller}/{action}/{id} ตัวอย่างเช่น

http://localhost:1234/home/index/7
domain name => http://localhost:1234
Controller class => home
Action method => index
parameter value => 7

ตัวอย่าง ของ coding Controller "Home" และด้านในมี Action "Index"

public class HomeController : Controller
    {
        public ActionResult Index(string id)
        {
            return View();
        }
    }

Controller class => HomeController (ใน url เราจะตัด Controller ออก ซึ่งในที่นี้คือ home)
Action method => Index

ตารางด้านล่างเป็นตัวอย่าง url (parameter value หรือ Id จะมีหรือไม่มีก็ได้)

URLControllerActionId
http://localhost/homeHomeControllerIndexnull
http://localhost/home/index/123HomeControllerIndex123
http://localhost/home/aboutHomeControllerAboutnull
http://localhost/home/contactHomeControllerContactnull
http://localhost/studentStudentControllerIndexnull
http://localhost/student/edit/123StudentControllerEdit123
     ซึ่ง url แบบนี้ค่อนข้างที่จะทำให้เราอ่าน url ได้ง่ายกว่าแบบเก่ามาก เพราะมี level ที่เป็นระบบระเบียบ และเราสามารถที่จะกำหนด pattern ของ url ได้เองอีกด้วย แต่กระผมจะขอไม่อธิบายเพราะปกติเราจะไม่ค่อยได้เข้าไปปรับแต่งซักเท่าไหร่ หากสนใจก็สามารถเข้าไปอ่านได้ ASP.NET Configure Route

สอบถามหรือติดตามได้ที่ https://www.facebook.com/learnaspnetmvcjquery

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Part2 -> Section1 The Basic -> 003. MVC Folder Structure

Part2 -> Section1 The Basic -> 001. Introduction

สารบัญ (Table of content)