ASP. net mvc 3.0 (I): summary of the new features of MVC 3.0
ASP. net mvc 3.0 (II): MVC concept and MVC 3.0 development environment
ASP. net mvc 3.0 (III): first recognized mvc url ing rules Routing
ASP. net mvc 3.0 (4): I want to configure routing for MVC rules.
ASP. net mvc 3.0 (V): Start With Controller/Action
ASP. net mvc 3.0 (6): Create your view in MVC 3.0
ASP. net mvc 3.0 (7): The New razor engine of MVC 3.0
ASP. net mvc 3.0 (8): passing and saving your model in MVC 3.0
ASP. net mvc 3.0 (9): MVC 3.0 verifies your model
ASP. net mvc 3.0 (10): MVC 3.0 uses forms authentication
ASP. net mvc 3.0 (11): Use filter in MVC 3.0
ASP. net mvc 3.0 (12): MVC 3.0 uses custom HTML controls
ASP. net mvc 3.0 (13): MVC 3.0 prevents cross-site Request Forgery (csrf) Attacks
ASP. net mvc 3.0 (14): create a data table in the MVC 3.0 instance Series
ASP. net mvc 3.0 (15th): Sorting of tables in the MVC 3.0 instance Series
ASP. net mvc 3.0 (16): pages of table data in the MVC 3.0 instance Series
ASP. net mvc 3.0 (17): filtering data in tables of MVC 3.0 instances
ASP. net mvc 3.0 (18): Combined sorting, paging, and filtering of tables in the MVC 3.0 instance Series
ASP. net mvc 3.0 (19th): using open-source controls for MVC 3.0 instances to sort and pagination tables
Overview
We have learned the MVC routing rules. After accepting the URL request, the MVC routing rule matches the action method with the requested controller to perform the operation.
Controller Optimization
In ASP. NET applications that do not use the MVC Framework, user interaction is generally organized in the following aspects:
Page, events triggered by PAGE and page control, and events triggered by PAGE and page control.
In contrast, in ASP. net mvc applications, user interaction is organized around controllers and operation methods.
The controller defines the operation method. The controller can include multiple operation methods as needed.
There is usually a one-to-one ing between operation methods and user interactions. For example, user interaction includes entering a URL in a browser, clicking a link, and submitting a form. Each of these user interactions sends a request to the server. In each case, the requested URL contains the information that the MVC framework uses to call an operation method.
Analysis
When a user inputs a URL into a browser, the MVC application analyzes the URL and determines the Controller path using the routing rules defined in the global. asax file. Then, the Controller determines the appropriate operation method to process the request. By default, the requested URL is considered as a sub-path, which contains the Controller name followed by the Operation name. For example, if you enter the URL http://contoso.com/mywebsite/products/categories, the sub-region is /products/categories. The default routing rule regards "Products" as the prefix name of the Controller (the end must be "controller", such as productscontroller ). It regards "categories" as the name of the operation. Therefore, this routing rule calls the categories method of the products controller to process the request. If the URL ends with/products/detail/5, the default routing rule regards "detail" as the operation name and calls the detail method of the products controller to process the request. By default, the value "5" in this URL is passed as a parameter to the detail method.
Basic Concepts
How to execute MVCController?
The ASP. net mvc framework maps URLs"Controllers". The controller processes incoming requests, user input and interaction, and executes the corresponding application logic. The Controller class usually calls a separate view component to generate HTML tags for requests.
The base class for all controllers is the controllerbase class, which can be used for common MVC processing. The Controller class inherits from controllerbase and is the default Implementation of the controller. The Controller class is responsible for the following processing stages:
Find the corresponding operation method to call and verify whether the method can be called.
Obtain the value of the parameter to be used as an operation method.
Handle all possible errors during the execution of the operation method.
Provides the default webformviewengine class used to present ASP. NET page types (Views.
Controller class
Provides methods to respond to HTTP requests made to ASP. net mvc websites.
Webformviewengine class
A view engine used to present a web form page to the response.
Create a controller
In the solution's controller folder, right-click -- add --- Controller
We can see that the above Code returns an actionresult-type view, but we have not yet created a view corresponding to it ..
So we want him to output a string to the page...
Code
public ActionResult Index()
{
return View();
}
Change
The following code
A simple controller named homecontroller contains an action named index, which outputs Hello word to the page...
publicclass HomeController : Controller
{
//
// GET: /Home/
publicstring Index()
{
return"Hello World";
}
}
Effect
Actionresult return type
Most operation methods return instances of classes derived from actionresult. The actionresult class is the basis of all operation results. However, there are different operation result types, depending on the task executed by the operation method. For example, the most common operation is to call the view method. The view method returns an instance of the viewresult class derived from actionresult.
You can create operation methods for returning objects of any type (such as strings, integers, or boolean values. These return types are encapsulated in the appropriate actionresult type before being rendered to the response stream.
The following table shows the built-in operation result types and the operation helper methods that return these types.
Operation Result |
Helper Method |
Description |
Viewresult |
View |
Displays the view as a webpage. |
Partialviewresult |
Partialview |
Displays the branch view, which defines a part of a view that can be displayed in another view. |
Redirectresult |
Redirect |
Use its URL to redirect to another operation method. |
Redirecttorouteresult |
Redirecttoaction Redirecttoroute |
Redirect to another operation method. |
Contentresult |
Content |
Return the User-Defined content type. |
Jsonresult |
JSON |
Returns the serialized JSON object. |
Javascriptresult |
Javascript |
Returns the scripts that can be executed on the client. |
Fileresult |
File |
Returns the binary output in the response to be written. |
Emptyresult |
(None) |
The return value used when the operation method must return a null result (void. |
Non-action Method
By default, the MVC framework treats all public methods of the controller class as operation methods. If your controller class contains a public method and you do not want it to be an operation method, you must mark the method with the nonactionattribute feature.
The following example shows how to mark with the nonaction feature.
Nonactionattribute class
Indicates a feature used to indicate that the Controller method is not an operation method.
[NonAction]
privatevoid DoSomething()
{
// Method logic.
}
Action Method with Parameters
By default, the value of the operation method parameter is retrieved from the request data collection. Data collection includes form data name-value pairs, query string values, and cookie values.
The Controller class searches for the operation method and determines all the parameter values of the Operation Method Based on the routedata instance and form data. If the parameter value cannot be analyzed and its type is reference or null, null is passed as the parameter value. Otherwise, an exception is thrown.
Get Parameters
You can use multiple methods to access the URL parameter value in the Controller operation method. The Controller class exposes the request and response attributes that can be accessed in the operation method. These attributes have the same semantics as httprequest and httpresponse objects that are already part of ASP. NET. However, the request and response objects of the controller class will accept objects that implement the httprequestbase and httpresponsebase abstract classes (instead of being sealed classes. By using these base classes, you can easily create a mock object, so that you can easily create unit tests for the Controller class.
The following code
The example shows how to use the request object to retrieve the query string value named ID.
// URL:
/// Home/detail? Id = 5
Publicstring detail ()
{
VaR id = convert. toint32 (request ["ID"]);
Return "The received parameter is" + id. tostring ();
}
Effect
Automatic ing operation method parameters
The ASP. net mvc framework can automatically map URL parameter values to the parameter values of the operation method.
By default, if the operation method uses parameters, the MVC Framework checks the incoming request data and determines whether the request contains an HTTP request value of the same name. If it contains, the request value is automatically passed to the operation method.
The following code:
Demonstrate the variations in the preceding example. In this variation, it is assumed that the ID parameter is mapped to the request value whose name is also id. Because of this automatic ing, the operation method does not need to include the code used to obtain the parameter value from the request, so the parameter value is easier to use.
// URL:
/// Home/detail? Id = 5
/// Home/detail/5
Publicstring detail (int id)
{
Viewdata ["detailinfo"] = ID;
Return "The obtained parameter is:" + viewdata ["detailinfo"]. tostring ();
}
Effect
You can also embed a parameter value as part of a URL rather than as a query string value. For example, you can use URLs such as/products/detail/3 instead of URLs containing query strings, such as/products/detail? Id = 3. The default routing ing rule is in the format of/{controller}/{action}/{ID }. If a URL sub-path exists after the controller and operation name in the URL, the sub-path is treated as a parameter named ID and is automatically passed as a parameter value to the operation method.
The MVC framework also supports optional parameters for operation methods. The optional parameters in the MVC Framework are real parameters that can be null type by using the Controller operation method. For example, if the method can use date as a part of the query string, but you want to use the default value of the date when the query string parameter is missing, you can use code similar to the Code in the following example:
If the request includes the value of the date parameter, the value is passed to the showarticles method. If the request does not contain the value of this parameter, the real parameter is null, and the controller can use any operation required to process the missing parameter.
The Code is as follows:
// URL:
/// Home/showarticles? Date = 2011-11-11
/// Home/showarticles
Publicstring showarticles (datetime? Date)
{
If (! Date. hasvalue)
{
Date = datetime. now;
}
Return "date parameter:" + date. value. tostring ();
}
Result 1 has Parameters
Effect 2 No Parameters
Summary
The Controller is responsible for retrieving model data and passing the model to the view object. It notifies the view object to display. Or it can directly output prompt information...
In ASP. net mvc, a controller can contain multiple actions. Each action is a method and an actionresult instance is returned.
The actionresult class includes the executeresult method. This method is executed when the actionresult object returns.
Turn: Memories of lost youthHttp://www.cnblogs.com/lukun/