Transferred from: http://kb.cnblogs.com/page/102191/
ASP. NET MVC 3 supports a new view engine option named "Razor" (in addition to continuing to support/strengthen the existing. aspx view engine). When writing a view template, razor minimizes the number of characters and keystrokes required and guarantees a fast, unobstructed coding workflow.
Unlike most templates, with the help of razor, you do not need to interrupt code writing just to label the beginning and end of the server-side code block in HTML. The code parser is smart enough to infer from your code whether it is a server-side code. This more concise, expressive syntax is cleaner and the input is faster and more interesting.
Today's blog covers a feature that many people don't know about razor--using @helper syntax to define reusable helper methods.
A simple @helper method application scenario
The @helper syntax in Razor makes it easy to create reusable helper methods that encapsulate output functionality in your view template. They make it easier to reuse code and make the code more readable. Let's look at a super-simple scenario that shows how the @helper syntax is being used.
The code before we define the @helper method
Let's look at a simple product List application scenario. In this scenario, we list the product details and output the product price or the word "Free!" "-If the product does not cost anything:
The above code is straightforward, and the razor syntax makes it easy to integrate server-side C # code in HTML.
However, a bit confusing place is the if/else logic of price. We may export prices (or on the same page) elsewhere in the site, and copying the above logic is error-prone and difficult to maintain. A similar scenario is the preferred consideration for using @helper syntax extraction and refactoring as helper methods.
Refactoring the above example using @helper syntax
Let's extract the price output logic and encapsulate it inside a helper method that we'll name "Displayprice". We can do this by rewriting the following code sample:
We have used the @helper syntax above to define a reusable helper method named "Displayprice". Just like the standard c#/vb method, it can contain any number of parameters (you can also define null or optional parameters). However, unlike the standard C#/vb method, the @helper method can contain both content and code and support the full razor syntax-which makes it very easy to define and encapsulate rendering/formatting helper methods.
You can call the @helper method just as you would call a standard C # or VB method:
When this method is called, Visual Studio provides IntelliSense code:
Reusing @helper in multi-view mode
In the above example, we define the @helper method as the code that invokes it in the same view template. Alternatively, we can define the @helper method outside the view template and ensure that it is reusable in all view templates in the project.
You can save our @helper method in. cshtml/.vbhtml and put this file under the \App_Code directory created under the project root for example, I created a "scottgu.cshtml" file in the \App_Code folder, and 2 separate helper methods are defined in the file (you can have any number of helper methods in each file):
Once our helpers are defined at the application level, we can use them in any of the application's view templates.
The 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 rewrite the previous example to invoke it:
Visual Studio provides IntelliSense code when you invoke an application-level helper as follows:
May 15 Update: Some people point out that when a @helper is saved in the \App_Code directory, you cannot access the ASP. HTML helper method in it by default. (e.g. Html.ActionLink (), Html.textbox (), etc.). And when they are defined in the same folder as the view, you can access the built-in HTML helper methods. When the helper is in the \App_Code directory, it does not support access to the built-in HTML helper method at the moment-we will add this feature in the next release. Paul Stovall has a good helper class that you can access and use simultaneously with the built-in HTML methods in the @helper method that you define in the \App_Code directory. Learn more about how to use this information here.
Summarize
Razor's @helper syntax provides an easy way to encapsulate rendering functionality into a helper method. You can reuse it in a single view template or in all view templates for an entire project.
You can use this feature to write cleaner, easier-to-maintain code.