Original article: http://kb.cnblogs.com/page/102191/
ASP. net mvc 3 supports a new view engine option named "Razor" (in addition to continuing to support/enhance the existing. aspx view engine ). When writing a view template, Razor minimizes the number of characters and keys required and ensures a fast and smooth coding workflow.
Unlike the syntax of most templates, with the help of Razor, you do not need to interrupt code writing, just to mark the beginning and end of the server-side code block in HTML. The Code Analyzer is smart enough to infer from your code whether it is server-side code. This more concise and expressive syntax is cleaner, and the input is faster and more interesting.
Today's blog covers a feature that many people do not know about Razor-using the @ helper syntax to define reusable helper methods.
Simple @ helper method application scenarios
The @ helper syntax in Razor allows you to easily create reusable helper methods, which can encapsulate output functions in your view template. They enable better code reuse and make the code more readable. Let's look at a super simple application scenario, which shows how the @ helper syntax is used.
Code before we define the @ helper Method
Let's look at a simple product list application scenario. In this scenario, we will list the product details and output the product price or the word "free !" -- If this product does not cost anything:
The above code is very straightforward, and the Razor syntax enables simple integration of server-side C # code in HTML.
However, a bit confusing is the price of the if/else logic. We may output prices (or on the same page) at other locations on the site, and copying the above logic is prone to errors and difficult to maintain. A similar application scenario is to use @ helper syntax to extract and refactor the helper method.
Use the @ helper syntax to refactor the above example
Let's extract the price output logic and encapsulate it in a helper method that we will name as "DisplayPrice. We can perform this operation by overwriting the following code example:
We have used the @ helper syntax above to define the reusable helper method named "DisplayPrice. Like the Standard C #/VB method, it can contain any number of parameters (you can also define parameters as null or optional ). However, unlike the Standard C #/VB method, the @ helper method can contain both the content and code and support the complete Razor syntax. This makes it very easy to define and encapsulate the rendering/formatting helper method.
You can call the @ helper method like calling a standard C # Or VB method:
When this method is called, Visual Studio provides the SMART awareness code:
Use @ helper in multi-view mode
In the above example, we define the @ helper method as the code to call it in the same view template. Alternatively, we can define the @ helper method outside the view template and ensure that it can be used repeatedly in all view templates of the project.
You can. cshtml /. vbhtml saves our @ helper method and stores this file in the \ App_Code directory created under the project root directory. For example, I created a "ScottGu. the cshtml file defines two separate helper methods (in each file, you can have any number of helper methods ):
Once our helper is defined at the application level, we can use them in any view template of the application.
ScottGu. cshtml in the \ App_Code folder above is logically compiled into a class called "ScottGu. This class contains static members of "DisplayPrice" and "AnotherHelper. We can use the following code to override the previous example to call it:
When you call the application-level helper using the following method, Visual Studio provides intelligent awareness code:
Updated on April May 15: Some people pointed out that when a @ helper is saved in the \ app_code directory, you cannot access the ASP. net mvc Html helper method by default. (For example, Html. ActionLink (), Html. TextBox (), and so on ). When they are defined in the same folder as the view, you can access the built-in HTML helper method. When the Helper is located in the \ app_code directory, it is true that access to the built-in HTML helper method is not supported now -- we will add this function in the next release. Paul Stovall has a good helper class that you can access and use at the same time and the built-in Html method defined in the @ helper method under the \ app_code directory. Please learn from here