Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: What are Action Method? Action Result? How are they related?

I'm sorry to ask such a basic question, but it's kind of fundamental for me. To better understand filters, I need to understand this notions. Although I'm on ASP.NET MVC for few months now and now are doing nice demos, I'm more familiar with Action method concept than action result.

What are:

  1. Action Method?
  2. Action Result?
  3. How are they related?

Let's say I've this

public ViewResult ShowPerson(int id)
{
  var friend = db.Persons.Where(p => P.PersonID == id).First(); 
  return View(friend);
}

How those concepts apply to the above code?

Thanks for helping.

like image 326
Richard77 Avatar asked Jul 17 '10 11:07

Richard77


People also ask

What are action methods in ASP.NET MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

What is action result method in MVC?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result. RedirectResult - Represents a redirection to a new URL.

What is the difference between action result and view result in MVC?

ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.


2 Answers

In your example ShowPerson is the action. Each action needs to return an action result (In your case it returns a view). So when a controller action method is invoked it does some processing and decides what view would be best adapted to represent the model.

There are many different action results that you might use. They all derive from ActionResult:

  • ViewResult - if you want to return a View
  • FileResult - if you want to download a file
  • JsonResult - if you want to serialize some model into JSON
  • ContentResult - if you want to return plain text
  • RedirectResult - if you want to redirect to some other action
  • HttpUnauthorizedResult - if you want to indicate that the user is not authorized to access this action
  • FooBarResult - a custom action result that you wrote
like image 190
Darin Dimitrov Avatar answered Sep 22 '22 10:09

Darin Dimitrov


Answer by @Darin-dimitrov is very much upto the point. But I see explanation given on MSDN also very much helpful.

Action methods typically have a one-to-one mapping with user interactions. Examples of user interactions include entering a URL into the browser, clicking a link, and submitting a form. Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.

When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global.asax file to parse the URL and to determine the path of the controller. The controller then determines the appropriate action method to handle the request. By default, the URL of a request is treated as a sub-path that includes the controller name followed by the action name. For example, if a user enters the URL http://contoso.com/MyWebSite/Products/Categories, the sub-path is /Products/Categories. The default routing rule treats "Products" as the prefix name of the controller, which must end with "Controller" (such as ProductsController). It treats "Categories" as the name of the action. Therefore, the routing rule invokes the Categories method of the Products controller in order to process the request. If the URL ends with /Products/Detail/5, the default routing rule treats "Detail" as the name of the action, and the Detail method of the Products controller is invoked to process the request. By default, the value "5" in the URL will be passed to the Detail method as a parameter.

like image 42
Mitesh Budhabhatti Avatar answered Sep 19 '22 10:09

Mitesh Budhabhatti