Asp.net MVC helper and user-defined functions @ functions Summary
The asp.net Razor view has the. cshtml suffix, which allows you to easily switch between c # code and html tags, greatly improving our development efficiency. However, the Razor syntax still has some cotton candy which is worth learning about. It can greatly improve our development efficiency and reduce the emergence of development bugs. Razor uses the @ tail symbol, which improves the development efficiency of Mvc. The following describes two reusable helper and functions related. As a modern programmer, we try our best to abide by one principle. Do not repeat yourself. So we will merge all the code that can be reconstructed, but this is for the background code C #. Some simple business logic can also be implemented at the View layer, of course, the View layer can perform complex business logic judgment, but the predecessors say that complicated business logic is the work of Model or Controller. The task of the View layer is to display, and the business logic should be less, the better. In the View layer, what are some refactoring operations? One of them is the @ helper custom fragment. For example, we want to output a number. If it is 0, the output does not exist. If it is another number, the output exists. Of course, this can be easily done under the powerful Razor syntax. 1 @ (ViewBag. IsEnabled = "0 "? "Does not exist": "does exist") But what if there are many such logic judgments on the current page? Intelligent programmers must know that they cannot repeat your own principles, so we need to refactor them. But how can we refactor them on the View layer? Use @ helper to solve this problem. Copy code 1 @ helper Show (int count) 2 {3 if (count = 0) 4 {5 @: 6} 7 else 8 {9 @: 10} 11} 12 13 @ (ViewBag. isEnabled = 0? "Does not exist": "exists") 14 @ Show (0) @ * Call helper * @ to copy the code so that we can call it at multiple locations on the current page for output, if you want to modify it, you can modify part of it, not all of it. What else should I do if I want to use this helper on other pages? Of course there are still some solutions. Add a View File under the app_code folder (assumed as UIHelper. cshtml), copy the helper code, and then run @ UIHelper on the View page to be called. show (0. Because the files in the app_code folder are eventually compiled into classes. Summary: We will summarize the number of implementation methods for output based on different situations. Of course, what I can think of is not completely. Use helper for global settings so that all pages to be judged call this helper method. Determine the code in the background and output it to the foreground View. You can obtain it through Html. Action () or Html. Partial (). Of course, if you simply use helper, you can use this method in a complicated way. It is implemented through the custom function Functions. User-Defined Function @ functions: the user-defined function uses the c # syntax to achieve code reuse, but this function can output html tags to the page. Copy code 1 // User-Defined Function @ functions 2 @ functions {3 public IHtmlString Get (int count) 4 {5 string result = ""; 6 if (count = 0) 7 {8 result = "no"; 9} 10 else11 {12 result = "exist"; 13} 14 return new HtmlString (result ); 15} 16} 17 18 19 @ Get (0) // call the custom function copy code. Please note that @ functions corresponds to the code segment of Razor. You need to add {}, in addition, functions is a formal c # method. If you want to use this function on multiple pages at the same time, you can port this method to app_code, assuming the file name is UIHelper. cshtml. And the method must be defined as static. This is easy to understand. UIHelper is equivalent to the class name, and functions is equivalent to the method. If you want to call it through the class name. method name, you must define the method as static. UIHelper. copy cshtml File Code 1 @ helper ShowUnit (int count) 2 {3 if (count = 0) 4 {5 @: free 6} 7 else 8 {9 @ count10} 11} 12 13 @ functions {14 public static IHtmlString Check (int count) 15 {16 string result = ""; 17 if (count = 0) 18 {19 result = "fsdfsdfsdfd"; 20} 21 else22 {23 result = count. toString (); 24} 25 return new HtmlString (result); 26} 27} 28 // custom function @ functions29 @ functions {30 public static IHtm LString Get (int count) 31 {32 string result = ""; 33 if (count = 0) 34 {35 result = "no "; 36} 37 else38 {39 result = "exist"; 40} 41 return new HtmlString (result); 42} 43} copy the code summary: helper is designed to directly output html content and has simple logic. helper does not return any values, while functions custom functions are much more powerful. If functions needs to return html content, the returned value is of the IHtmlString type. If the returned value is not required, it can be set to void. However, if no value is returned, the meaning of the function is lost. Therefore, the returned values are generally IHtmlString. For the reconstruction of the View layer, we can use helper and user-defined function functions.