แอ็ตทริบิวต์ ActionName คือตัวเลือกการดำเนินการซึ่งใช้สำหรับชื่ออื่นของวิธีการดำเนินการ เราใช้แอตทริบิวต์ ActionName เมื่อเราต้องการเรียกวิธีการดำเนินการนั้นด้วยชื่ออื่นแทนชื่อจริงของวิธีการ
[ActionName("AliasName")] คอนโทรลเลอร์
ตัวอย่าง
using System.Collections.Generic;
using System.Web.Mvc;
namespace DemoMvcApplication.Controllers{
public class HomeController : Controller{
[ActionName("ListCountries")]
public ViewResult Index(){
ViewData["Countries"] = new List<string>{
"India",
"Malaysia",
"Dubai",
"USA",
"UK"
};
return View();
}
}
} ดู
@{
ViewBag.Title = "Countries List";
}
<h2>Countries List</h2>
<ul>
@foreach(string country in (List<string>)ViewData["Countries"])
{
<li>@country</li>
} ในข้างต้นเนื่องจากเราได้ระบุชื่อการดำเนินการที่แตกต่างกันสำหรับวิธีดัชนี เมื่อเราพยายามนำทางด้วยชื่อการดำเนินการ ดัชนีจะได้รับข้อผิดพลาด 404

ตอนนี้ให้เราลองนำทางโดยใช้ชื่อการดำเนินการของ ListCountries
