views:

19

answers:

1

Hello,

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.

+1  A: 

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
Darin Dimitrov
@Darin Dimitrov: I've a better understanding now. So, an action Result is just a regular method. I can even see, for instance View(object). Thanks very much.
Richard77
@Richard77, an action result is the return type of a regular method (which is called action). `View(object)` is simply a method defined on the `Controller` class that returns a `ViewResult`.
Darin Dimitrov
@Darin Dimitrov: Ok, I see even better.
Richard77