Working with Action Selectors in MVC

Action selectors are attribute in MVC. It is used with action method and in routing by routing engine to handle a request.

  1. ActionName
  2. NonAction
  3. ActionVerbs

1. ActionName:

ActionName attribute allow us to specify different action name than method name.

[ActionName(“GetData”)]

public ActionResult About(){

return View();

}

Now URL will be http://localhost:port/controler/GetData/. Action name “About” is not be consider in URLrouting.

View Name:

Now here question arises what will be name of view. View name should be GetData otherwise it will throw error. You can pass view name “About” inside view method then it will work.

Ex: View(“About”);

[ActionName(“GetData”)]

public ActionResult About()

{

return View(“About”);

}

2. NonAction:

All action method should be public. Sometimes we do not want method to be treated as action method and it should not be called by routing engine. We use NonAction attribute for this purpose.

public class HomeController : Controller{

[NonAction]

public ActionResult GetData(){

return View();

}

}

3. Action Verbs(Accept Verbs):

MVC supports different kind of action verbs like HttpGet, HttpPost, HttpPut, HttpDelete,

HttpOptions and HttpPatch. These attributes are used with action method to support Http request.


Summary:

  • Attribute selectors are use in URL routing by MVC routing engine.
  • ActionName attribute is used to specify different action name than method name.
  • There are three action selectors available in MVC
    • ActionName
    • NonAction
    • ActionVerbs
  • NonAction method marks the action method as public method and cannot be used by routing engine.

Be the first to comment

Leave a Reply

Your email address will not be published.


*