ASP.Net MVC-তে Action Method হল একটি মেথড যা কন্ট্রোলারে সংজ্ঞায়িত থাকে এবং এটি HTTP রিকোয়েস্ট গ্রহণ করে এবং একটি রেসপন্স প্রদান করে। Action Methods সাধারণত ব্যবহারকারীর অনুরোধ অনুসারে ডেটা প্রক্রিয়াকরণ, ভিউ রেন্ডারিং বা অন্য কোনো সার্ভিস কল করার জন্য ব্যবহৃত হয়। প্রতিটি অ্যাকশন মেথড একটি নির্দিষ্ট কাজ সম্পাদন করে এবং একটি Action Result রিটার্ন করে, যা ভিউ, রিডাইরেক্ট, JSON বা অন্যান্য ধরণের রেসপন্স হতে পারে।
ASP.Net MVC-তে Action Method তৈরি করতে কন্ট্রোলারে একটি পাবলিক মেথড লিখতে হয়, যা একটি HTTP রিকোয়েস্ট হ্যান্ডল করে। Action Method সাধারণত একটি return type হিসেবে ActionResult
অথবা এর কোনো সাবক্লাস রিটার্ন করে।
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
এখানে, Index
একটি Action Method যা ActionResult
টাইপ রিটার্ন করে। এই মেথডটি Home/Index
URL প্যাটার্নের সাথে মিলে গেলে চালু হয় এবং এর মাধ্যমে View
রিটার্ন করা হয়।
Action Method এর মাধ্যমে বিভিন্ন ধরণের রেসপন্স রিটার্ন করা যেতে পারে। এখানে কিছু সাধারণ return types এর উদাহরণ দেয়া হলো:
public ActionResult About()
{
return View();
}
public ActionResult RedirectToHome()
{
return RedirectToAction("Index", "Home");
}
public JsonResult GetData()
{
var data = new { Name = "Rahim", Age = 30 };
return Json(data, JsonRequestBehavior.AllowGet);
}
public ActionResult LoadPartialView()
{
return PartialView("_MyPartialView");
}
public ContentResult GetContent()
{
return Content("This is some plain text content.");
}
Action Method-এর মধ্যে ইনপুট প্যারামিটারও থাকতে পারে, যা HTTP রিকোয়েস্ট থেকে পাওয়া ডেটা গ্রহণ করে। উদাহরণস্বরূপ, URL প্যারামিটার বা ফর্ম ডেটা Action Method-এ পাস করা যেতে পারে।
public ActionResult Greet(string name)
{
return Content("Hello, " + name);
}
এখানে, Greet
Action Method একটি name
প্যারামিটার গ্রহণ করে এবং একটি কাস্টম গ্রীটিং রিটার্ন করে।
[HttpPost]
public ActionResult SubmitForm(string name, int age)
{
// Process the form data
return View();
}
এটি একটি POST রিকোয়েস্ট গ্রহণ করে এবং ফর্ম ডেটা (যেমন, name
এবং age
) Action Method-এ পাস করে।
ASP.Net MVC Action Method-এর ওভারলোডিংও সাপোর্ট করে, যা একই নামের বিভিন্ন মেথড তৈরি করতে দেয়, তবে প্রত্যেকটি আলাদা প্যারামিটার থাকে।
public ActionResult Show(int id)
{
// Handle the request with an integer parameter
return View();
}
public ActionResult Show(string name)
{
// Handle the request with a string parameter
return View();
}
এখানে দুটি Show
Action Method রয়েছে, একটি int
প্যারামিটার নিয়ে এবং আরেকটি string
প্যারামিটার নিয়ে কাজ করে।
ASP.Net MVC-তে Action Method-এর উপর বিভিন্ন ধরনের Attributes ব্যবহার করা যায়, যা Action Method-এর আচরণ নিয়ন্ত্রণ করতে সাহায্য করে।
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(FormCollection form)
{
// Process the form data
return RedirectToAction("Index");
}
এখানে, HttpGet
এবং HttpPost
অ্যাট্রিবিউট ব্যবহার করা হয়েছে, যা নির্দিষ্ট HTTP মেথডের জন্য Action Method নির্ধারণ করে।
ASP.Net MVC-তে Action Method হল এমন মেথড যা ব্যবহারকারীর অনুরোধ গ্রহণ করে এবং সঠিক রেসপন্স প্রদান করে। Action Method এর মাধ্যমে ভিউ রেন্ডার করা, রিডাইরেক্ট করা, JSON ডেটা পাঠানো, পার্শিয়াল ভিউ রেন্ডারিং করা ইত্যাদি কাজ করা যায়। Action Methods-এর মাধ্যমে বিভিন্ন রিটার্ন টাইপ ব্যবহার করা সম্ভব, এবং প্যারামিটার গ্রহণ করেও অ্যাকশন মেথডে ডেটা প্রক্রিয়া করা যায়। এর মাধ্যমে ডেভেলপাররা অত্যন্ত কার্যকরভাবে HTTP রিকোয়েস্ট পরিচালনা করতে পারেন।
common.read_more