ASP. net mvc learning controller scalability

Source: Internet
Author: User
Tags time in milliseconds

The Controller has been mentioned in the previous article, and today's notebook is used as an extension. Ii. Body 1. I believe that you will encounter the cognominal problem of the Action method during the development process. Although the name of the method is the same as that of the parameter, the logic in the method is different, because you have set the corresponding annotation attribute to determine which action method to call. In this case, you need to separate the action name from the method name, so you can use the ActionName annotation attribute. For example, if we want a page to display different pages during local access and non-local access, but you want to separate them using different methods, then you can use this annotation attribute. For example, the following code: Copy code 1 [ActionName ("Index")] 2 public ActionResult LocalIndex () 3 {4 return View (); 5} 6 7 public ActionResult Index () 8 {9 return View (); 10} copy the code although the name of the first method is LocalIndex, however, the final page name is still queried Based on the name set in the ActionName. Therefore, do not create a view based on the method name. This is wrong. 2. from the beginning to the present, we have learned that as long as the public method written in the controller is an action method (simply put, it corresponds to a page ), but sometimes we need a public method, but it is not an action method, just to facilitate unit testing, what should we do? ASP. net mvc thinks about this and provides NonAction annotation attributes for us. It is very simple to use, so we will not give an example separately. 3. The first section of the Custom Action Method selector introduces how the Controller determines which action method to execute, which can be the same name and the same parameters? One of them is to filter the matching action methods according to the return value of the action method selector (that is, the annotation attribute of the Action method), ASP. net mvc has HttpPost and so on, of course we can also customize, as long as the IsValidForRequest method in the following abstract class is implemented: Copy code 1 [AttributeUsage (AttributeTargets. method, AllowMultiple = false, Inherited = true)] 2 public abstract class ActionMethodSelectorAttribute: Attribute 3 {4 protected ActionMethodSelectorAttribute (); 5 6 public abstract bool IsValidForRequest (ControllerContext controllerContext, M EthodInfo methodInfo); 7} 8 9 where MethodInfo contains information about action methods. 10 11 The following is an example of the author. You can decide whether to execute this action based on the form value: 12 public class LoginAttribute: ActionMethodSelectorAttribute13 {14 bool _ isFirst; 15 16 public LoginAttribute (bool isFirst) 17 {18 _ isFirst = isFirst; 19} 20 21 public override bool IsValidForRequest (ControllerContext controllerContext, System. reflection. methodInfo methodInfo) 22 {23 24 string obj = controllerContext. httpContext. request. form ["loginType"]; 25 if (Obj! = Null) 26 {27 if (_ isFirst) 28 {29 if (obj. equals ("true") 30 return true; 31} 32 else33 {34 if (obj. equals ("false") 35 return true; 36} 37} 38 return false; 39} 40} copy code 4. asynchronous controllers are believed to be engaged in ASP. NET users will certainly know the concept of Asynchronization, which is not just in ASP. NET, even in ASP. net mvc also exists, only slightly changed. If the controller contains an asynchronous method, we need to inherit the AsyncController class and divide the asynchronous action method into two parts, the first method to execute an asynchronous operation must be named action method + Async. After the asynchronous operation is Completed, the action method name + Completed method will be called. Pay special attention to the method specifications, if the name is incorrect, you may not be able to see that the asynchronous controller is correct. The following is a simple example: Copy code 1 namespace MvcStudy. controllers 2 3 {4 5 [SessionState (System. web. sessionState. sessionStateBehavior. disabled)] 6 7 public class TestController: AsyncController 8 9 {10 11 [NoAsyncTimeout] 12 13 public void IndexAsync () 14 15 {16 17 AsyncManager. outstandingOperations. increment (); 18 19 Task. factory. startNew () => 20 21 {22 23 Thread. sleep (2000); 24 25 AsyncManager. parameters ["da Ta "] =" test "; 26 27 AsyncManager. outstandingOperations. decrement (); 28 29}); 30 31} 32 33 34 35 public ActionResult IndexCompleted (string data) 36 37 {38 39 ViewBag. data = data; 40 41 return View (); 42 43} 44 45} 46 47} copy code here we use the Increment method to enable an asynchronous operation (this method can pass a number to indicate several asynchronous operations to be enabled ), then, we use StartNew to start an asynchronous operation. After the asynchronous operation is complete, we put the data in Parameters so that we can get it through the IndexCompleted parameter, the Decrement method is called to identify the completion of an asynchronous operation. There is also an annotation attribute (NoAsyncTimeout) in the IndexAsync action method, which indicates that the asynchronous operation has no timeout limit. If you need to set a timeout, you can use AsyncTimeout, and input a time in milliseconds. If the asynchronous operation times out, a TimeOutException exception occurs. Readers will be confused about what the annotation attribute SessionState is, and everyone will be familiar with ASP. NET, by default, the general handler is not allowed to access the Session. The objective is to improve the performance, so in ASP. net mvc, we can use SessionState to make the Controller do not need to maintain the Session. Of course, we will not be able to access the Session in this controller, but it can improve the performance.

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.