![]() ![]() Just like a standard C#/VB method, it can contain any number of arguments (you can also define the arguments to be either nullable or optional). We’ve used the syntax above to define a reusable helper method named “DisplayPrice”. We can do this by re-writing the sample to the code below: Let’s extract the price output logic, and encapsulate it within a helper method that we’ll name “DisplayPrice”. Refactoring the above sample using the syntax Scenarios like this are prime candidates to extract and refactor into helper methods using the syntax. We will likely output prices elsewhere within the site (or within the same page), and duplicating the above logic everywhere would be error-prone and hard to maintain. One place that is a little messy, though, is the if/else logic for the price. The above code is fairly straight-forward, and Razor’s syntax makes it easy to integrate server-side C# code within the HTML. ![]() Let’s look at a simple product listing scenario where we list product details, and output either the price of the product – or the word “FREE!” if the item doesn’t cost anything: Let’s look at a super-simple scenario of how the syntax can be used. They enable better code reuse, and can also facilitate more readable code. The syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. Today’s blog post covers a cool feature of Razor that a lot of people don’t know about – which is the ability to define re-usable helper methods using the syntax. ![]() Implicit and Explicit code nuggets with Razor.You can learn more about Razor from some of the blog posts I’ve done about it over the last last 9 months: This enables a compact and expressive syntax which is clean, fast and fun to type. The Razor parser is smart enough to infer this from your code. Unlike most template syntaxes, with Razor you do not need to interrupt your coding to explicitly denote the start and end of server blocks within your HTML. Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow. ASP.NET MVC 3 supports a new view-engine option called “Razor” (in addition to continuing to support/enhance the existing. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |