ASP. net mvc (controller, controller action, and action result)-part.3

Source: Internet
Author: User

From Zhang Ziyang: http://www.cnblogs.com/JimmyZhang/archive/2009/01/03/1367644.html

This tutorial explores the topics of ASP. net mvc controller, controller action, and action result. After reading this tutorial, you will understand how the controller controls the way visitors interact with ASP. net mvc websites.

1. Understand the Controller

The MVC Controller is responsible for responding to requests initiated to ASP. net mvc websites. Each browser request is mapped to a dedicated controller. For example, suppose you entered the following URL in the address bar of your browser:

Http: // localhost/product/index/3

In this case, a controller named productcontroller is called. Productcontroller is responsible for generating responses to browser requests. For example, the Controller may return a specific view or redirect the user to another controller.

You can use ASP. NET MVCProgramAdd a new Controller under the controllers folder to create a new controller. Right-click the Controller folder, select "add" and "new" from the menu, and select "MVC controller class (MVC controller class) "(see figure 1 ). The Controller name must contain the Controller suffix. For example, there is no problem with the Controller name productcontroller, but the Controller product does not work.

Figure 1-create a new Controller

If you create a new controller named productcontroller, you will obtainCodeThe file shown in Listing 1.

Code List 1-productcontroller. CS

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. Web;

UsingSystem. Web. MVC;
NamespaceMvcapp. controllers {

Public Class Productcontroller: Controller

{

PublicActionresult index ()

{

// Add action logic here

Throw NewNotimplementedexception ();

}

}

}

As you can see in code list 1, the controller is just a class (Visual Basic. Net or C # class ). A controller is a class inherited from the system. Web. MVC. controller base class. Since the Controller inherits from this base class, the Controller easily inherits some useful methods (which will be discussed soon ).

2. Understand the Controller action

The Controller exposes the Controller action. Action is a controller method. when you enter a specific URL in the browser address bar, this method is called. For example, if you send a request to the following URL:

Http: // localhost/product/index/3

In this example, the index () method is called on the productcontroller class. The index () method is an example of the controller action.

A controller action must be a public method of the controller class. C # method. By default, it is a private method. Realize that any public method you add to the Controller class will be automatically exposed as a controller action (you must be very careful because the Controller action can be called by anyone around the world, simply enter the correct URL in the address bar of the browser ).

The Controller action also meets some additional requirements. Methods Used as controller actions cannot be reloaded. In addition, the Controller action cannot be a static method. In addition, you can use any method as a controller action.

3. Understand the Controller results

Controller action returns a type calledAction result(Action result. The action result is what the Controller action returns to the browser for request.

The ASP. net mvc Framework supports six standard types of action results:

    1. Viewresult-Indicates HTML and tag.
    2. Emptyresult-indicates no result.
    3. Redirectresult-Indicates redirecting to a new URL.
    4. Redirecttorouteresult-Indicates redirecting to a new controller action.
    5. Jsonresult-represents a JSON (JavaScript Object Notation) result, which can be used in Ajax applications.
    6. Contentresult-indicates the text result.

All these action results are inherited from the actionresult base class.

In most cases, the Controller acts viewresult. For example, the index () controller action in code list 2 returns a viewresult.

Code List 2-bookcontroller. CS

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. Web;

UsingSystem. Web. MVC;
NamespaceMvcapp. controllers {

Public Class Bookcontroller: Controller

{

PublicActionresult index ()

{

ReturnView ();

}

}

}

When an action returns a viewresult, HTML is returned to the browser. The index () method in code list 2 returns a view named index. aspx to the browser.

Note that the index () Action in Listing 2 does not return a viewresult (). Instead, the view () method of the controller base class is called. Generally, you do not directly return an action result. Instead, it calls one of the following methods of the controller base class:

    1. View-returns a viewresult.
    2. Redirect-returns the result of a redirectresult action.
    3. Redirecttoaction-returns the result of a redirecttoaction.
    4. Redirecttoroute-returns the result of a redirecttoroute action.
    5. JSON-return the result of a jsonresult action.
    6. Content-returns the result of a contentresult action.

Therefore, if you want to return a View to the browser, you can call the view () method. If you want to downgrade a user from one controller action to another, you can call the redirecttoaction () method. For example, the details () Action in code listing 3 either displays a view or redirects the user to the index () Action, depending on whether the ID parameter contains a value.

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. Web;

UsingSystem. Web. MVC;
NamespaceMvcapp. controllers {

Public Class Customercontroller: Controller

{

PublicActionresult details (Int? ID)

{

If(ID =Null)

ReturnRedirecttoaction ("Index");

ReturnView ();

}
PublicActionresult index ()

{

ReturnView ();

}

}

}

The contentresult action result is very special. You can use the contentresult action result to return the action result as plain text. For example, the index () method in code listing 4 returns messages as plain text instead of HTML.

Code list 4-statuscontroller. CS

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. Web;

UsingSystem. Web. MVC;
NamespaceMvcapp. controllers {

Public Class Statuscontroller: Controller

{

PublicContentresult index ()

{

ReturnContent ("Hello world! ");

}

}

}

When statuscontroller. Index () is called, no view is returned. Instead, the original text "Hello World!" is returned to the browser !".

If a controller action returns a result that is not an action result-for example, a date or integer-the result is automatically wrapped in contentresult. For example, when you call the index () Action of workcontroller in code listing 5, the date is automatically returned as a contentresult.

Code List 5-workercontroller. CS

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. Web;

UsingSystem. Web. MVC;
NamespaceMvcapp. controllers {

Public Class Workcontroller: Controller

{

PublicDatetime index ()

{

ReturnDatetime. now;

}

}

}

The index () Action in code listing 5 returns a datetime object. The ASP. Net MVC Framework automatically converts a datetime object to a string and encapsulates the datetime value in a contentresult. The browser will receive the date and time in plain text.

4. Summary

The purpose of this tutorial is to introduce the concepts of controller, controller action, and controller action result in ASP. net mvc. In the first part, you learned how to add a new controller to the ASP. net mvc project. Next, you learned how the public method of the controller is exposed to the world as a controller action. Finally, we discuss different types of action results, which can be returned from the Controller action. In particular, we have discussed how to return a viewresult, redirecttoactionresult, and contentresult from a controller action.

Related Article

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.