ASP. NET Controller and action method

Source: Internet
Author: User

1. What kind of class can become a controller?

in ASP. NET MVC, a class that implements the IController interface directly or indirectly is considered a controller by the MVC framework.

using System.Web.Routing; namespace system.web.mvc{    publicinterface  icontroller    {        void Execute (RequestContext requestcontext);}    }

From the code, see that this interface has a unique method of execute, which is called when the request arrives at the controller.
If we define a class implementation to implement this interface directly, it means that we have to write a lot of code to handle the request. Fortunately, the MVC framework defines a controller abstract class for us, and we can derive a large amount of logic to process the request directly, and we just need to define the action method.

How does the 2.MVC framework know which controller to use?

The MVC framework extracts the value of the key as "controller" from the Routedata.values, which is the name of the controller, which is the controller that MVC is looking for.

3. Where does the controller access the input data?

Mainly from three places: 1) extracts data from a context object. 2) data that is formed as a parameter to the action method. 3) explicitly call the model binding attribute of the MVC framework.

1) When I derive my controllers from the Controller abstract class, we automatically get a batch of convenient properties that can access and request the relevant data, which are Request,response,routedata,httpcontext, Server.

2) ① uses the action method parameter, MVC does not allow the action method has the ref back out parameter, MVC encounters this situation, throws the exception directly.

The ②MVC framework will help me automatically assign values to the action method without requiring us to manually check the context object for assignment. The assignment of an MVC to an action method is also done by examining the context object.

These context objects include request.querystring,request.form and routedata.values. The processing of the name of the parameter is not case-sensitive. For example, a parameter of the action method is city, then request.querystring["City" or request.querystring["city" can be.

How does the ③ parameter object be instantiated? The underlying controller class uses components called "Value providers" and "model bindings" to get the parameter values for an action method.

④ is in the MVC framework; An action method is also called if a value of a parameter of a reference type cannot be found. However, the value of this parameter is set to NULL, and it can be said that the parameter of the reference type is optional.

If the type of the argument of an action method is a value type, MVC cannot find the value of the parameter and throws an exception. You can say that the parameter of a value type is required. To make the parameter of a value type optional,

There are 2 ways to make a parameter nullable type. or set a default value for the parameter.

⑤ If you do not want to handle a request without an action method parameter, but you do not want to check for null or throw an exception in your code, you can use an optional parameter attribute of C # instead: Specify the default for the parameter.

 Public ActionResult Index (string query="all",int page=1) { // other Code }

3. How does the controller produce the output?

The MVC framework separates the indicated intent from the execution intent by using the action result. Instead of using the response object directly, we return an object derived from the ActionResult class. This object describes the functions that the controller responds to complete. For example, render a view or redirect to another URL or action method, and so on.

When an MVC framework receives an ActionResult object from an action method, the MVC framework invokes the Executeresult party defined by the class. R then change the implementation of the action result to process the response object and produce the output that matches your intent.

There are many built-in action result types in the MVC framework that are derived from ActionResult, many of which have convenient methods in the Controller class.

---------

The MVC framework finds the order of view locations:

( the application uses a region to find the view from here.)

/areas/<areaname>/views/<controllername>/<viewname>.aspx

/areas/<areaname>/views/<controllername>/<viewname>.ascx

/areas/<areaname>/views/shared/<viewname>.aspx

/areas/<areaname>/views/shared/<viewname>.ascx

/areas/<areaname>/views/<controllername>/<viewname>.cshtml

/areas/<areaname>/views/<controllername>/<viewname>.vbhtml

/areas/<areaname>/views/shared/<viewname>.cshtml

/areas/<areaname>/views/shared/<viewname>.vbhtml

(The application does not use the area to find from here, or use the area, but the view is not found earlier, it will go to the following places to find)

/views/<controllername>/<viewname>.aspx

/views/<controllername>/<viewname>.ascx

/views/shared/<viewname>.aspx

/views/shared/<viewname>.ascx

/views/<controllername>/<viewname>.cshtml

/views/<controllername>/<viewname>.vbhtml

/views/shared/<viewname>.cshtml

/views/shared/<viewname>.cshtml

(controllername): The controller name name is removed from the string after the "controller" string.

As soon as the ①MVC frame finds the view in one place, it stops the find action, and the found view is used to render the response to the client.

The directory sequence for the ②MVC framework Search view is "contract better than Configuration". You do not need to register the view file, you only need to put the view file in a set of known locations, the MVC framework can recognize them.

The actual name of the ③ view is determined by the value of the routedata.value["action".

④ we can provide a clear path to the view method and bypass the search phase to complete the specified view. When specifying a view, the path must start with "~/" or "/" and include the file's extension.

---------

How to pass data from an action method to a view.

⑴ provides a model object to the view.

You can send an object to the view as a parameter of the view method.

 Public ViewResult Index () {   DateTime dt=datetime.now;    return View (DT);}

This allows us to access this object in the view using the Model keyword.

⑵ Passing Data with ViewBag

This feature allows you to define any property on a dynamic object and access it in the view.

⑶ Passing Data using ViewData

The ViewData feature is similar to the view Bag feature, which is implemented using the Viewdatadictionary class rather than a dynamic object.

The Viewdatadictionary class is similar to a rule's "key/value" collection.

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.