1.

What is the Action Method in MVC?

Answer»

The action methods in ASP.NET MVC will help for executing the request and generating a response to it. Every PUBLIC method of the MVC Controller is considered an action method. If you want the public method to act as a non-action method, then it is possible to decorate the action method by using the ATTRIBUTE “NonAction”. This will restrict an action method from rendering on the browser.

A few points to be remembered while working with action methods are:

  • The action method is the public and non-static method
  • It cannot be the protected or private method
  • It cannot be an extension method
  • It can’t have ref and out parameters
  • It cannot be overloaded

It is possible to create action methods that will return an object of any type like an integer, a Boolean value, or a string. These return types will be wrapped in an ActionResult type that is appropriate before they are being RENDERED onto the response stream. Any return type that is not an action result will be converted into a string by the ASP.NET MVC and the string will be rendered into the browser. We can create a simple controller as PER the code given below:

public class HomeController : Controller { public ActionResult HelloInterviewbit() { ViewData["SayInterviewbit"] = "Interviewbit"; return View(); } public string SayInterviewbit() { return "This is from 'SayInterviewbit'."; } }

We must create a view for running SayInterviewbit() as it will return the View as views\home\SayInterviewbit.cshtml. For executing the second action method, it does not require the creation of a view. In the browser, the string will be rendered. It is possible for an void public method to act as an action method. This implies that any public method can BECOME an action method.



Discussion

No Comment Found