Part 67 - Action selectors in mvc - ActionName AcceptVerbs

Actions are public methods in an mvc controller that responds to an URL request. You can control or influence which action method gets invoked using action selectors in mvc. Action selectors are attributes that can be applied to an action method in a controller. 

ActionName selector: This action selector is used when you want to invoke an action method with a different name, than what is already given to the action method. 

For example, the following URL request would invoke Index() action method in HomeController
/Home/Index

public class HomeController Controller
{
    public string Index()
    {
        return "Index action method invoked";
    }
}

If you want to invoke Index() action method, with the following URL
/Home/List

Then decorate the action method with ActionName attribute as shown below.
public class HomeController Controller
{
    [ActionName("List")]
    public string Index()
    {
        return "Index action method invoked";
    }
}

Now, if you navigate to /Home/Index, you will get an error - The resource cannot be found. 

At the moment, the Index() action method is returning a string, but if it returns a view, should the view be named - Index or List.?
[ActionName("List")]
public ActionResult Index()
{
    return View();
}

List should be the view name. If for some reason, you want to use "Index" as the view name, then modify the controller action method as shown below.
[ActionName("List")]
public ActionResult Index()
{
    return View("Index");
}

AcceptVerbs selector: Use this selector, when you want to control, the invocation of an action method based on the request type. In the example below, the "Edit" method that is decorated with GET acceptverb responds to the GET request, where as the other "Edit" method responds to POST request. The default is GET. So, if you don't decorate an action method with any accept verb, then, by default, the method responds to GET request.
public class HomeController Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Edit(int id)
    {
        Employee employee = GetEmployeeFromDB(id);
        return View(employee);
    }
    
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(Employee employee)
    {
        if (ModelState.IsValid)
        {
            // Save employee to the database
            return RedirectToAction("Index");
        }
        return View(employee);
    }
}

HttpGet and HttpPost attributes can be used as shown below. This is an alternative to using AcceptVerbs attribute.
public class HomeController Controller
{
    [HttpGet]
    public ActionResult Edit(int id)
    {
        Employee employee = GetEmployeeFromDB(id);
        return View(employee);
    }
    
    [HttpPost]
    public ActionResult Save(Employee employee)
    {
        if (ModelState.IsValid)
        {
            // Save employee to the database
            return RedirectToAction("Index");
        }
        return View(employee);
    }
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值