ASP. NET controller and action method, asp.net controller action

Source: Internet
Author: User

ASP. NET controller and action method, asp.net controller action

1. What kind of class can become a controller?

In ASP. net mvc, classes that directly or indirectly implement the IController interface are considered as controllers by the Mvc framework.

using System.Web.Routing;namespace System.Web.Mvc{    public interface IController    {        void Execute(RequestContext requestContext);    }}

The Code shows that this interface has a unique method called Execute, which is called when the request arrives at the controller.
If we define a class to implement this interface directly, it means that we must write a lot of code to process the request. Fortunately, the Mvc Framework defines a Controller abstract class for us. We can directly derive this class to obtain a large number of request processing logic. We only need to define the action method.

2. How does the Mvc Framework know which controller to use?

The Mvc Framework extracts the value with the key "controller" from RouteData. Values. This value + "Controller" forms the name of the controller, that is, the Controller that Mvc is looking.

3. Where does the Controller Access the input data?

Mainly from three places: 1) extract data through context objects. 2) data generated as a parameter passed to the action method. 3) explicitly call the model binding feature of the Mvc framework.

1) When I derive my Controller from the Controller abstract class, we will automatically get a batch of convenient attributes that can access and Request-related data. These attributes include Request, response, RouteData, HttpContext, Server.

2) ① using the action method parameters, Mvc does not allow the action method to have the ref return out parameter. In this case, Mvc directly throws an exception.

② The Mvc Framework will automatically assign values to action methods, without manually checking the context object. Mvc assigns a value to the Action Method to check the context object.

These context objects include Request. QueryString, Request. Form, and RouteData. Values. Parameter names are not differentiated by size. For example, if a parameter of the Action method is city, either Request. QueryString ["city"] or Request. QueryString ["City"] can be used.

③ How are parameter objects instantiated? The underlying Controller class uses components called "value provisioner" and "model binding" to obtain parameter values for Action methods.

④ In the Mvc framework, if the value of a parameter of the reference type cannot be found, the action method is also called. However, the value of this parameter is set to null. It can be said that the parameter of the reference type is optional.

If the parameter type of the Action method is value type and Mvc cannot find the value of this parameter, an exception is thrown. It can be said that a value type parameter is required. To make the parameter of the Value Type optional,

There are two methods: one is to define the parameter as a void type. Or set the default value for the parameter.

⑤ If you do not want to handle requests that do not contain action method parameters, but do not want to check for null in the code or throw an exception, you can use an optional parameter feature of C # To specify the default value for the parameter.

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

3. How does the Controller generate output?

The Mvc Framework separates the specified intent from the execution intent by using the Action Result. Instead of directly using the Response object, we return an object derived from the ActionResult class. This object describes the functions that the controller responds. For example, rendering a view or redirecting to another URL or action method.

When the Mvc Framework receives an ActionResult object from an action method, the Mvc Framework calls the ExecuteResult party defined by this class. R and then change the implementation of the action result to process the Response object and generate the output that meets your intention.

There are many built-in action result types in the Mvc framework. These types are derived from ActionResult, and many of them have convenient methods in the Controller class.

---------

The order of view positions in the Mvc framework:

(The application uses a region to search for the view)

/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

(If the application does not use a region, it can start searching from here or use a region, but the view is not found in the morning, and the following area will be searched)

/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 removes the string after the "Controller" string.

① As long as the Mvc Framework finds the view in one position, it will stop searching, and the view found will be used to render the response to the client.

② The Directory sequence of the Mvc Framework search view is "Convention is better than configuration ". You do not need to register view files. You only need to place view files in a group of known locations, and the Mvc framework can recognize them.

③ The actual view name is determined based on the Value of RouteData. Value ["action.

④ We can provide a clear path for the View method and bypass the search phase to complete the specified View. When specifying a view, the path must be /"Or"/", and contains the file extension.

---------

How to pass data from the Action Method to the view.

(1) provide a model object for 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);}

In this way, we can access this object using the Model keyword in the figure.

(2) Use ViewBag to transmit data

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

(3) Use ViewData to transmit data

The ViewData feature is similar to the View Bag feature. It is implemented using the ViewDataDictionary class, not a dynamic object.

The ViewDataDictionary class is similar to the rule's "key/value" set.

 


How does aspnet mvc specify a view for controller actions?

Call View overload when return: View ("View name ");

Aspnet developed version controller,

Haha, select me! I personally recommend using vss. After all, it is developed by Microsoft and has good compatibility. Download A vss installation package from the Internet. Check the operation instructions and configure it!
 

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.