2010.9.8 su Peng
Content
-Call and attribute of Action
Prerequisites
-Install Visual Studio 2010 Express
-Understand ASP. Net
-Understand the basic concepts of the Design Mode
Call Action
After obtaining the Url, Routing returns to RouteTable to match the Url. After matching, the Controller and Action can be found.
-ControllerActionInvoker
It can complete four operations:
1. Find the corresponding Action
2. Find the parameter sent from the route corresponding to the current request and send it to Action after matching.
3. Call all Filters of the Action method (Filters is a constraint on Action. Only Filters can know whether Action can be executed)
4. Call ExecuteResult
Action matches Url
-Match the Action name from the url
Action and Route are invisible. They all communicate with each other through the ControllerActionInvoker.
It first needs to find the Dictionary class in the Route and find the key of the Action, so that the Action name can be found here.
Action Selection Method
By default, reflection is usually used to find Action methods. This is also the reason that the Action method must be public. If it is not public, the name cannot be obtained, and the following work cannot be completed.
Method Standard:
-NonActionAttribute flag is not allowed.
-Architecture functions, attribute controllers, and event accessors cannot be used as the action method.
-Methods inherited from object or Controller
These three criteria are verified by ActionSelectorAttribute.
ActionNameAttribute
Action has a naming rule. If an Action method is named XXXCompleted, this method must be called asynchronously. If this is the name of the method to be synchronously called, you must add an alias "ActionName" to the Action. In this way, the ActionName will not match the function name, but will match the marked name.
ActionSelectorAttribute
-ActionSelectorAttribute Definition
The function of this class is to verify IsValidFromRequest for the final match. If the Matching content is False, this Action will be eliminated from the list. If the Matching content is True, this Action will be verified. At last, there will be one matching result in our list. This function is finally called. If more than one matching result is found after traversal, an exception is thrown. If no Action matches after traversal, an exception is thrown.
AcceptVerbsAttribute
It is the simplest Filters. It is a method that combines two types of Http requests. This can verify whether the request meets the specific Action. This method can also solve the problem of duplicate function names.
Simulate Rest requests
HttpPostAttribute corresponding Creation Mode
HttpPutAttribute corresponds to the update mode
Reading mode corresponding to HttpGetAttribute
Delete mode corresponding to HttpDeleteAttribute
With this correspondence, we can use a Url for CRUD (Create, Read, Update, and Delete. In this way, the consistent interfaces available for Web applications in the Rest architecture are further standardized, and the operations on resources depend on the Http method.
Ing parameters
Three parameter passing methods:
1. RequestFormCollection
2. RouteData (recommended)
3. QueryString
Once the Action method is determined, the parameter of the Action needs to be mapped.
Call Action
-Use Asynchronous Action
When IIS is started, there will be a thread pool. When a user initiates a request, IIS will find an idle thread from the thread pool and say you will respond to the request, this thread is designed to respond to user requests. For example, if the user request takes 2 seconds to complete, the server will implement blocking within 2 seconds and wait for the request to complete. This thread is not only the thread that responds to the user request, it is also the thread that returns the user result. After the thread executes the operation and returns it to the user end, it returns to the thread pool because it is idle again. By default, because the thread pool has many Idle threads, this will have no impact. However, if there are many requests and IIS is too busy, IIS will not be able to process your requests. In this case, the network access is busy.
Therefore, we sometimes need to work in asynchronous mode. When the server receives the request, the Action wakes up a thread and the thread starts working. After the thread starts the work, it returns to the thread pool, and the work is done by other working threads in the background. After the work is completed, the MVC Architecture will tell IIS that my work is finished. If you give me another thread, I can return the result to you. There is no difference in this method, but the percentage of Idle threads on the server is relatively high.
Synchronous and asynchronous comparison
Use synchronization mode
1. Quick and short operations
Asynchronous operations need to obtain the thread again when returning results. This performance consumption should also be considered.
2. High Availability
Asynchronous Action is sent and returned by different threads, which is difficult to implement during testing and cannot be traced.
3. This operation requires high CPU, not high IO.
Use asynchronous operations
1. the test shows that this operation is a bottleneck for website application performance.
In addition to asynchronous Action, OutputCahce can be used to cache the Action results with OutputCahce. This kind of cache can effectively shorten the waiting time of users and return repeated requests sent by users with the cached results. In this way, data may not be updated in a timely manner, but the effect is still good. There is no difference between asynchronous requests and users.
2. High concurrency requirements
3. High IO rather than high CPU requirements for this operation
IO operations are the most prone to problems with the overall performance decline. Because IO access is dedicated, IO access is forwarded by the operating system.
Write asynchronous Action
Synchronous access
Asynchronous access
The asynchronous access Controller must inherit from AsyncController. The end of the Action name can only be Async and Completed. The two actions are a pair. The former responds to the Action request, and the latter returns data to the user. The two cannot be called directly as actions.
In asynchronous mode, Parameters are transmitted in different ways. You need to use the AsynvManager. Parameters dictionary class to save the results for return. Because synchronization is a thread, while Asynchronization is two threads, after the first thread ends, data will be lost. I must use an intermediate variable to transmit data between the two threads, this intermediate variable is AsynvManager, which is used to monitor the implementation of multiple variables.
OutstandingOperation. the Increment method is used to manage the current request of MVC. This operation is very important. It mainly allows the MVC Architecture to know how many operations are currently suspended. Once the work is handed over to the background of the Action, we don't care. Every time OutstandingOperation completes a corresponding Action, it will be-1. When it becomes 0, we will know that the Controller's work has been completed. The corresponding Completed method will be called.
Parallel Operation Performance
Synchronous access
Asynchronous access
Analyze asynchronous operations. OutstandingOperation. Increment (3) indicates three more operations. Once each operation is completed, you need to call OutstandingOperation. Decrement to set the operand-1. After all, there is a Completed function, which obtains all the current information, generates and returns. The above three operations are parallel, so the total execution time is the longest of the three operations, and the total execution time of the synchronization operation is the sum of the time of the three operations. This is the parallel advantage in asynchronous access. If an Action has multiple contents, it should be done in Asynchronous Parallel.
Use tags for asynchronous requests
The tag of asynchronous Action cannot be added to the Completed method.
Timeout
AsyncTimeout times out in 45 seconds by default. The Timeout label can change the Timeout time. NoAsync never times out (not recommended ). If this parameter is directly specified on the Controller, this label is added by default for each Action.
Additional instructions on Asynchronous Method calls
If you must use the synchronization method of the Completed suffix, you must use an alias.
If the Action method is asynchronous, but some steps in the method need to be synchronized, you can use the in and End functions to do it.
Note that Html. Action and Html. RenderAction can point to an asynchronous method, but this method will be called synchronously.
Update Model layer UpdateModel
Before the update, verify whether the ModelState is available before it is available. After the update, Redirect the Action to Edit. The reason for selecting Redirect to other places rather than directly returning a View layer is a trick. Sometimes, when a user submits data and does not respond, he will go back to refresh and refresh the data again. When the Post request comes over, once it is completed, he will jump to another Edit, you can edit the data or make other corresponding requests. Each time it is refreshed, ModelState becomes unavailable. At this time, only the results of the current request are returned, avoid repeated data submission when you refresh the page with the mouse.
Verify data
This is written in the Model layer.
Security
Never use user input without processing (it is necessary to use Html. Encode)
Summary
-Call and attribute of Action
2010.10.1