ActionName एट्रिब्यूट एक एक्शन सेलेक्टर है जिसका उपयोग एक्शन मेथड के लिए एक अलग नाम देने के लिए किया जाता है। हम ActionName एट्रिब्यूट का उपयोग तब करते हैं जब हम चाहते हैं कि एक्शन मेथड को उसके वास्तविक नाम के बजाय किसी अलग नाम से बुलाया जाए।
सिंटैक्स
[ActionName("AliasName")]
उदाहरण: कंट्रोलर
using System.Collections.Generic;
using System.Web.Mvc;
namespace DemoMvcApplication.Controllers
{
public class HomeController : Controller
{
// यह एक्शन "ListCountries" नाम से एक्सेसिबल होगा, "Index" से नहीं
[ActionName("ListCountries")]
public ViewResult Index()
{
ViewData["Countries"] = new List
{
"India",
"Malaysia",
"Dubai",
"USA",
"UK"
};
return View();
}
// एक ही नाम के दो एक्शन होने पर भी उपयोगी (ओवरलोडिंग के लिए)
[ActionName("GetCountry")]
public JsonResult GetCountryById(int id)
{
return Json(new { Id = id, Name = "Country " + id }, JsonRequestBehavior.AllowGet);
}
[ActionName("GetCountry")]
public JsonResult GetCountryByName(string name)
{
return Json(new { Name = name }, JsonRequestBehavior.AllowGet);
}
}
}
व्यू (Index.cshtml)
@{
ViewBag.Title = "Countries List";
}
Countries List
@foreach (string country in (List)ViewData["Countries"])
{
- @country
}
रूटिंग व्यवहार
गलत URL - 404 एरर
// यह काम नहीं करेगा - 404 लौटाएगा
https://localhost:59146/Home/Index

सही URL - ActionName के साथ काम करता है
// यह काम करेगा
https://localhost:59146/Home/ListCountries

ActionName के सामान्य उपयोग मामले
1. RESTful URL पैटर्न
public class ProductsController : Controller
{
[ActionName("List")]
public ActionResult Index() { ... }
[ActionName("Detail")]
public ActionResult Details(int id) { ... }
[ActionName("Create")]
[HttpGet]
public ActionResult CreateForm() { ... }
[ActionName("Create")]
[HttpPost]
public ActionResult CreateSubmit(Product model) { ... }
}
2. HTTP वर्ब के आधार पर अलग नाम
[ActionName("Users")]
[HttpGet]
public ActionResult GetUsers() { ... }
[ActionName("Users")]
[HttpPost]
public ActionResult CreateUser(UserModel model) { ... }
3. लिगेसी URL सपोर्ट
// पुराना URL: /Home/ShowProducts
// नया एक्शन नाम: Index
[ActionName("ShowProducts")]
public ActionResult Index() { ... }
4. रिजर्व्ड कीवर्ड्स से बचना
// "Object" C# में रिजर्व्ड है, एक्शन नाम के रूप में उपयोग नहीं कर सकते
[ActionName("Object")]
public ActionResult GetObject(int id) { ... }
ActionName के साथ ActionLink/Url.Action
// व्यू में - ActionName में दिए गए नाम का उपयोग करें
@Html.ActionLink("देश देखें", "ListCountries", "Home")
// URL जनरेट करना
@Url.Action("ListCountries", "Home")
// परिणाम: /Home/ListCountries
NonAction एट्रिब्यूट के साथ तुलना
| एट्रिब्यूट | उद्देश्य | URL से एक्सेसिबल |
|---|---|---|
[ActionName("Alias")] | एक्शन का नाम बदलता है | हां, नए नाम से |
[NonAction] | पब्लिक मेथड को एक्शन नहीं बनाता | नहीं |
public class HomeController : Controller
{
[ActionName("CustomName")]
public ActionResult OriginalName() { return View(); } // /Home/CustomName से एक्सेसिबल
[NonAction]
public string HelperMethod() { return "Internal"; } // URL से एक्सेसिबल नहीं
}
नोट: ASP.NET Core में, [ActionName] को [HttpGet("custom-name")] या [Route("custom-name")] एट्रिब्यूट रूटिंग के साथ बदल दिया गया है।