February 20, 2014

ASP.Net – วิธีการ map virtual path เป็น physical path

ก่อนอื่น, มาทำความเข้าใจว่าอะไรคือ physical path และ virtual path. ตรงนี้คือตัวอย่าง web application project แบบง่าย ๆ . ภายใน the project, มี page หนึ่งเรียกว่า PageInRootDirectory.aspx. อันดับแรกต้องเข้าใจว่าอะไรคือ root directory หรือ root web application directory.

project นี้สร้างบน physically ภายใน the hard disc. วิธีที่เราสามารถหา folder ภายใจ project ที่ ถูกสร้างใน physical project? จากนี้, คลิกขวามใน folder และเลือก Open Folder in Windows Explorer. มันจะเปิด physical folder เมื่อ project นี้ได้สร้างเรียบร้อย. ถ้าเราดูว่า project ที่สร้างอยู่ที่ไหน, เราสามารถเห็นว่ามนจะแสดงใน D:/SampleWeb/SampleWeb.



February 10, 2014

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor


นี้เป็นวิธีการในการ render partal view ใน MVC Razor. หาย ๆ นักพัฒนาคิดความสับสนว่าใช้งานอย่างไร ระหว่าง RenderPartial หรือ RenderAction หรือ Partial หรือ Action helper methods จากการ render ตัว partial view. ในบทความนี้ จะอธิบายความแตกต่างระว่าง @Html.RenderPartial, @Html.RenderAction, @Html.Partial และ @Html.Action



@Html.RenderPartial
1. method นี้ ผลลัพท์จะเป็นการ directly written แบบ Http response stream หมายความว่ามันถูกใช้เหมือนกับ TextWriter object เช่นเดียวกับการใช้ในา webpage/template ปัจจุบัน
2. ตัว method จะ return void
3. ใช้งานง่ายโดยไม่ต้องสร้างตัว Action ใด ๆ ใน Controller
4. method @Html.RenderPartial มีประโยชน์ในการใช้เมื่อแสดง data ใน partial view in already in the corresponding view model.

         @{ Html.RenderPartial("_Comments"); }

5. method นี้ เร็วกว่า method @Html.Partial  โดย result ถูกส่งข้อความกับเป็น response stream ทำให้ทำงานได้เร็ว