ASP.NET MVC: what is an action method? The result of the action? How are they connected?

Sorry to ask such a basic question, but for me it was fundamental. To better understand the filters, I need to understand these concepts. Although I have been on ASP.NET MVC for several months now and am doing nice demonstrations, I am more familiar with the concept of an action method than the result of an action.

What:

  • Action method?
  • The result of the action?
  • How are they connected?

Say I have it

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

How do these concepts apply to the above code?

Thanks for the help.

+7
asp.net-mvc action
source share
2 answers

In your example, ShowPerson is an action. Each action should return the result of the action (in your case, it returns a view). Therefore, when the controller action method is called, it does some processing and decides which view is best suited to represent the model.

There are many different action results that you can use. They all come from ActionResult :

  • ViewResult - if you want to return the view
  • FileResult - if you want to upload a file
  • JsonResult - if you want to serialize some model in JSON
  • ContentResult - if you want to return plain text
  • RedirectResult - if you want to redirect to another action
  • HttpUnauthorizedResult - if you want to indicate that the user does not have permission to access this action
  • FooBarResult - the result of the user actions you wrote
+9
source share

The answer to this question @ Darin-dimitrov is a lot. But I see the explanation given on MSDN is also very helpful.

Action methods usually have a one-to-one mapping to user interaction. Examples of user interaction include entering a URL into a browser by clicking a link and submitting a form. Each of these user interactions results in a request being sent to the server. In each case, the request URL includes information about what the MVC framework uses to invoke the action method.

When a user enters a URL into a browser, the MVC application uses the routing rules that are defined in the Global.asax file to parse the URL and determine the path of the controller. The controller then determines the appropriate action method for processing the request. From the default, the request URL is treated as an additional path that includes the name of the controller, followed by the name of the action. For example, if the user enters the URL http://contoso.com/MyWebSite/Products/Categories , subpath / Products / Categories. The default routing rule is “Products” as the name of the controller prefix that should end with “Controller” (for example, ProductController). He sees "Categories" as the name of an action. Therefore, the routing rule calls the Product Controller Method Categories to process the request. If the URL ends with / Products / Detail / 5, the default routing considers the Detail to be the name of the action, and the Detail method of the product controller is called to process the request. From the default value of "5" in the URL will be passed to the Detail method as a parameter.

+1
source share

All Articles