3. Understand controller, controller action, and actionresults

Source: Internet
Author: User

In this articleArticleWe will discuss ASP. NET MVCController,Controller actionAndAction result. After reading this article, we should understand how the control layer interacts with ASP. net mvc pages.
I. Understanding the control layer
The MVC control layer mainly responds to user requests from ASP. net mvc web sites. Requests from each browser are mapped to the corresponding controller. For example, when we enter the following URL in the address bar
Http: // localhost/product/index/3

The productcontroller controller is triggered. The productcontroller controller responds to the browser. For example, the Controller returns a View to the browser or redirects the Controller to another controller.
To add a new controller, apply it in ASP. net mvc.ProgramRight-click the controllers folder, and select "add"-"new item" in the pop-up menu to bring up the MVC controller Template Selection dialog box, as shown in. The Controller name must contain the Controller suffix. For example, whether the productcontroller is the correct controller name, while the product is incorrect.

Figure 1
For example, if we create a new controller named productcontroller, its initial Code As follows:
Listing 1-productcontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapp. Controllers
{
Public classProductcontroller:Controller
{
Public Actionresult Index ()
{
// Add action logic here
Throw new notimplementedexception ();
}
}
}
The code above shows that the controller is a class derived from system. Web. MVC. controller. It is because it is sent from this class, so the controller has several useful methods (I will discuss it later)

2. Understand the Controller action
The Controller action is a method in the Controller class, which can be called by the client browser URL.. For example, if the client sends the following browser request
Http: // localhost/product/index/3
The index () method of the productcontroller class is called.

The Controller action must be a public method of the controller class.Any public method in the controller is automatically treated as a controller action.
Note that the controller action cannot be overloaded, and the Controller action cannot be a static method.In addition, we can use any public method as the controller action.

(Original: Home of the gray WormHttp://hi.baidu.com/grayworm)
3. Understand action results
The Controller returns the action result, which is the result that the controller returns to the browser when responding to a browser request. . Here I call it the action result.
The ASP. net mvc Framework supports the following six action results:
Viewresult-Provides HTML and tags
Emptyresult-No results are provided.
Redirectresult-Redirect to a new URL
Redirecttorouteresult-Redirect a new controller action.
Jsonresult-The Ajax program provides a JavaScript Object Notation
Contentresult-Provide a text result

All these action results are derived fromActionresultClass
In most cases, the Controller returns the viewresult.
The following index () Action
Listing 2-bookcontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapp. Controllers
{
Public class bookcontroller: Controller
{
PublicActionresultIndex ()
{
ReturnView();
}
}
}
When viewresult is returned, HTML is returned to the client browser. In the code above, the index () method returns a view named index. aspx to the browser.

You can note that,In the above Code, viewresult () is not returned. Instead, the view () method of the controller base class is called. Generally, we do not directly return the action result, but call the following methods of the controller base class to return the result:
View-Return the result of a viewresult action.
Redirect-Returns the redirectresult action result.
Redirecttoaction-Returns the redirecttorouteresult action result.
Redirecttoroute-Returns the redirecttorouteresult action result.
JSON-Return the jsonresult action result.
Content-Return the contentresult action result.

We can see that, When we want to return a View to the client browser, we should call the view () method. For example If we want to redirect to another controller action, we should call the redirecttoaction () method. For example: The details () action in the following code dynamically determines whether to return the view or redirect to index () action based on the input parameters.
Listing 3-customercontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapp. Controllers
{
Public class Customercontroller: Controller
{
Public Actionresult Details ( Int? ID )
{
If (ID = NULL)
Return redirecttoaction ("Index ");
Return view ( );
}
Public Actionresult Index ()
{
Return view ();
}
}
}

The contentresult action result is special. You can use contentresult to return the action result to the client as text.For example, the index () method of the following code returns a piece of common text information instead of HTML text.
Listing 4-statuscontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapp. Controllers
{
Public classStatuscontroller: Controller
{
PublicContentresultIndex ()
{
ReturnContent ("Hello world! ");
}
}
}
When the statuscontroller. Index () action is called, the returned result to the browser is not a view, but a non-formatted text "Hello World ".
If the action returned by the controller is not an action result, such as date data or integer data, the result is automatically packaged as a contentresult action result. In the following code, when we call the index () action, a date is returned, which is automatically packaged as contentresult.

Listing 5-workcontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapp. Controllers
{
Public classWorkcontroller: Controller
{
PublicDatetimeIndex ()
{
ReturnDatetime. now;
}
}
}
In the above Code, the index () action returns a date-type object. ASP. net mvc Framework converts the date-type data into string data and automatically packs it into contentresult. The browser receives a non-formatted text.
(Original: Home of the gray Worm Http://hi.baidu.com/grayworm )

Summary
This article mainly introduces several concepts of ASP. net mvc controller, control action, and controller action result.
In the first part, we learned how to add a new controller to the ASP. net mvc project. Then we learned how to make the control method a controller action. Finally, we discuss the results of different types of actions returned from the Controller. In addition, we also discussed how to return viewresult, redirecttoactionresult, and contentresult from the Controller action.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.