Sunday, April 26, 2020

Action Methods in Asp.net MVC


Action Methods are like any other method of a class and typically have a one-to-one mapping with user interactions. when a user enters a URL into the Browser in client-side then MVC App uses routing rules which are defined in the Global.aspx in the server-side to parse the URL and to exactly checks the path of the controller. and handles the requests in the controller with appropriate Action methods. and all the methods of a controller class are called Action Methods. the Action Methods have some restrictions as below:
      • Action Methods must be Public.
      • ActionResult is the superclass of all the return type action methods in MVC
      • it can not have open generic types.
      • it can not be overloaded
      • it can not be constructor, setter, getter
      • it can not be a static method.
      • action method can not be an extension method
      • every controller has at least one default Index() method and returns the View page.  

Asp.net MVC  ActionResult :


 various types of action results are available in asp.net MVC as shown below.
    1. ViewResult                       --> It returns the View 
    2. RedirectResult                  --> it returns to specific URL
    3. RedirectToRoutResult        --> it redirects to action controller
    4. PartialViewResult              --> receives as a response for view engine
    5. JsonResult                        --> it returns the JSON Result
    6. JavaScriptResult               --> it returns the Json Result 
    7. FilePathResult                  --> it returns the file content 
    8. ContentResult                   --> it returns the string
    9. EmptyResult                     -->it returns the nothing

Example of Action Method :


public class  ATGController : Controller  
     {
          public ActionResult AllTechGeeks()
             {
                 ViewData["atg"] = "All Tech Geeks";
                 return View();
           }
     }     

The Following Image explains briefly about Action Methods.


Action methods In Asp.net MVC



No comments: