"asp. Net MVC Learning Notes"-controller and action (1)

Source: Internet
Author: User

This article references:http://www.cnblogs.com/willick/p/3331521.html

1. Inherit the IController interface, and the sample code prints the currently requested controller and action to the Browser:

 public classBasiccontroller:icontroller { public voidExecute (requestcontext Requestcontext) {stringController = (string) requestcontext.routedata.values["Controller"]; stringAction = (string) requestcontext.routedata.values["Action"]; RequestContext.HttpContext.Response.Write (string. Format ("Controller: {0}, Action: {1}", controller, action)); }}

A slightly more complex system, inheriting IController is a lot of work to do, and it is not recommended to inherit the IController interface directly.

2, the MVC framework of the System.Web.Mvc.Controller class, provides a sufficient practical features to facilitate our processing of requests and return Results. It provides a few key features:

    • Action Method: A Controller, its behavior is divided into several methods, usually a method corresponds to a request, and can be obtained through the method parameters to request the data passed Over.
    • Actionresult: you can return an object that describes the result of the action method execution, and the advantage is that you want to return a result to specify the corresponding return object, without caring how to execute and produce the Result.
    • Filters: through the C # feature, the processing of one kind of behavior (such as authorization and Authentication) is encapsulated to facilitate reuse between multiple Controller and Action methods.

3. The following three main sources of data are obtained from controllers:

    • A series of context Objects.

The most common context objects are as follows:

    • The arguments passed to the Action method.

the Controller class obtains the value of the parameter for the Action method through the value provider of the MVC framework and the model binder Component. value Provider provides a range of values that can be accessed in the controller, internally it passes from request.form, request.querystring, request.files, and Routedata.values data (a Collection of key values), and then passes the data to the model Binder,model Binder to try to match the data to the parameters of the action Method. The default model binder can be created and assigned to Any. NET type Object parameters (that is, The parameters of the Action method), including the collection and the custom Type.

    • Explicitly invokes the model binding attribute of the Framework.

4. ActionResult is the object that describes the result of the action method execution, and the advantage is that you want to return what results to specify the corresponding return object on the line, do not care how to use the response object to organize and produce results. ActionResult is an example of a command pattern that describes operations by storing and passing objects. When the MVC framework receives an ActionResult object from the Action method, it calls the Object's Executeresult method, which internally returns the output we want by Response Object. The MVC framework contains many ActionResult types, all of which inherit from the ActionResult class, most of which have simple methods in the Controller class:

In addition to the table listed, there are contentresult,fileresult,jsonresult and javascriptresult.

5. There are several ways to pass data from action to View:

    • View Model Object:
 // action   Public   ViewResult Index () {DateTime date  = D     atetime.now;  return   View (date),  // view Gets the   @model DateTime @{viewbag.title  =  index   " ;}   the day  is : @ Model.dayofweek 
    • ViewBag, which is a weak type of dynamic, is parsed when the program is run and is a new feature in MVC3, only valid in the current view .
    • ViewData, is a collection of dictionaries, is only valid in the current view , performance is higher than ViewBag, but the use of the need for type Conversion.
    • tempdata, also a dictionary collection, is typically used to temporarily cache content between two requests or to pass messages between pages, saved in the session , and then purged from the session after Use.

Typically use ViewBag or ViewData in the current View to pass temporary data between two requests with Tempdata. Since TempData is released after being used, it needs to be stored in other variables if you want to use the data in TempData two Times.

"asp. Net MVC Learning Notes"-controller and action (1)

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.