![]() ![]() Once our helpers are defined at the app level, we can use them within any view template of our application. For example, below I created a “ScottGu.cshtml” file within the \App_Code folder, and defined two separate helper methods within the file (you can have any number of helper methods within each file): cshtml/.vbhtml files that are placed within a \App_Code directory that you create at the root of a project. We can accomplish this by saving our methods within. Alternatively, we can define the method outside of our view template, and enable it to be re-used across all of the view templates in our project. In the example above, we defined our method within the same view template as the code that called it. Visual Studio will provide code intellisense when invoking the method: You can invoke methods just like you would a standard C# or VB method: Unlike standard C#/VB methods, though, methods can contain both content and code, and support the full Razor syntax within them – which makes it really easy to define and encapsulate rendering/formatting helper methods: ![]() 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. Plus, if I run the app it gives me a parser error saying the control tag isn't MVC 3 supports a new view-engine option called “Razor” (in addition to continuing to support/enhance the existing. When I move the source file outside of the App_Code/ directory and set it to compile, the designer recognizes it but Intellisense in an aspx file (I applied the directive in my web config) doesn't have it listed, even after I build the entire project. When I set the Build Action to Compile in the App_Code/ directory and tried running the code, I got a conflict saying that my control was defined in multiple assemblies - my DLL and a temporary DLL, which meant that the code being compiled in App_Code/ conflicted with my own build. I check Intellisense by typing System.Web., but nothing is listed. Problem is when I do this, it no longer recognizes any references to System.Web.*. I looked it up online and seems like the way to do this is to place the code in App_Code/ so it compiles on the fly, which also changes the Build Action to Content. I'm trying to define a custom server control with a source file (not a user control) and use it via the Designer. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |