Richard77
Richard77

Reputation: 21609

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.

Upvotes: 6

Views: 14335

Answers (2)

Mitesh Budhabhatti
Mitesh Budhabhatti

Reputation: 1623

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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

Upvotes: 11

Related Questions