MVC-controller, mvcviewcontroller

Source: Internet
Author: User

MVC-controller, mvcviewcontroller

 Introduction to MVC-controller

1 public class HomeController: Controller 2 {3 // <summary> 4 // request URL http: // localhost/Home/Index 5 // </summary> 6 [ActionName ("Default")] 7 public ActionResult Index () 8 {9 return View (); 10} 11 12}

The only special note is that if you use the Default "return View ()" method to return the ActionResult class, because the [ActionName ("Default")] attribute is applied, so MVC will look for "/Views/Home/Default. aspx "page instead of"/Views/Home/Index. aspx "page.

6Action Method Selector

6.1 NonAction attributes

If the NonAction attribute is applied to the Action Method in the Controller, even if the Action method is a public method, the ActionInvoke is notified not to select this Action for execution. This attribute is mainly used to protect specific public methods in the Controller from being published to the Web. Or, if you do not want to delete this method when the function is not yet developed, you can also apply this attribute, indicating "do not make it public ".

  [NonAction]        public ActionResult Index()         {           return View();         }

Modifying "public" in the Action Method to "privare" also achieves the same purpose

 6.2 HttpGet, HttpPost, HttpDelete, and HttpPut attributes

The HttpGet, HttpPost, HttpDelete, and HttpPut attributes are part of the Action Method selector. We will introduce the following program as an example. If the [httpPost] attribute is applied, this Action can be selected only when the client browser sends an http post request.

 [HttpPost]2         private ActionResult Index()3         {4             return View();5         }

On the contrary, if these attributes are not applied, the client browser will automatically select the corresponding Action if any HTTP verb is sent.

These attributes are often used when window data needs to be accepted. You can create two actions with the same name. One application uses the [HttpGet] attribute to display the window HTML, and the other application uses the [HttpPost] attribute to receive the values sent from the window,

7 ActionResult class

The ActionResult class is the result of Action execution, but the ActionResult does not contain the execution result, but contains the information required for the execution response. After the Action returns the ActionResult class, it is executed by MVC. First, let's look at the program code of the ActionResult abstract class. Only one ExecuteResult () method is defined in the ActionResult abstract class to execute the result.

The ActionResult defined by MVC is shown in the table:

The Controller auxiliary method in the table provides support for the returned ActionResult class in the Controller class. The following program can be used to jump to another page.

1      [HttpPost]2         public ActionResult Post(FormCollection c)3         {4             return new RedirectResult("/");5         }

At the end of this article, if you have any questions or suggestions, you can have a lot of discussions, original articles, and limited writing skills. If there are any mistakes in this article, please kindly advise.

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.