Razor
There are 10 entries for the tag
Razor
Changing a big organizations is a slow endeavor. But when people are passionate and persistent, change does happen. Three years ago, the ASP.NET MVC source code was released under an open source license. But at the time, the team could not accept any code contributions. In my blog post talking about that release, I said the following (emphasis added): Personally (and this is totally my own opinion), I’d like to reach the point where we could accept patches. There are many hurdles in the way, but if you went back in time several years and...
Given how central JavaScript is to many modern web applications, it is important to use unit tests to drive the design and quality of that JavaScript. But I’ve noticed that there are a lot of developers that don’t know where to start. There are many test frameworks out there, but the one I love is QUnit, the jQuery unit test framework. Most of my experience with QUnit is writing tests for a client script library such as a jQuery plugin. Here’s an example of one QUnit test file I wrote a while ago (so you know...
Donut caching, the ability to cache an entire page except for a small region of the page (or set of regions) has been conspicuously absent from ASP.NET MVC since version 2. Mmmmm, donuts! – Photo by Pzado at sxc.hu This is something that’s on our Roadmap for ASP.NET MVC 4, but we have yet to flesh out the design. In the meanwhile, there’s a new NuGet package written by Paul Hiles that brings donut caching to ASP.NET MVC 3. I haven’t tried it myself yet, so be forewarned, but judging by the blog post,...
As a web guy, I’ve slung more than my fair share of angle brackets over the tubes of the Internet. The Razor syntax quickly became my favorite way of generating those angle brackets soon after its release. But its usefulness is not limited to just the web. The ASP.NET team designed Razor to generate HTML markup without being tightly coupled to ASP.NET. This opens up the possibility to use Razor in many other contexts other than just a web application. For example, the help documentation for NuGet.exe is written using the Markdown format that is produced by NuGet.exe....
Yesterday, during my ASP.NET MVC 3 talk at Mix 11, I wrote a useful helper method demonstrating an advanced feature of Razor, Razor Templated Delegates. There are many situations where I want to quickly iterate through a bunch of items in a view, and I prefer using the foreach statement. But sometimes, I need to also know the current index. So I wrote an extension method to IEnumerable<T> that accepts Razor syntax as an argument and calls that template for each item in the enumeration. public static class HaackHelpers {
...
Layouts in Razor serve the same purpose as Master Pages do in Web Forms. They allow you to specify a layout for your site and carve out some placeholder sections for your views to implement. For example, here’s a simple layout with a main body section and a footer section. <!DOCTYPE html>
<html>
<head><title>Sample Layout</head>
<body>
<div>@RenderBody()</div>
<footer>@RenderSection("Footer")</footer>
</body>
</html>
In order to use this layout, your view might look like.
@{
Layout = "MyLayout.cshtml";
}
<h1>Main Content!</h1>
@section Footer {
...
David Fowler turned me on to a really cool feature of Razor I hadn’t realized made it into 1.0, Templated Razor Delegates. What’s that? I’ll let the code do the speaking. @{
Func<dynamic, object> b = @<strong>@item</strong>;
}
<span>This sentence is @b("In Bold").</span>
That could come in handy if you have friends who’ll jump on your case for using the bold tag instead of the strong tag because it’s “not semantic”. Yeah, I’m looking at you Damian . I mean, don’t both words signify being forceful? I digress.
Note that the delegate that’s...
Within a Razor view, you have access to a base set of properties (such as Html, Url, Ajax, etc.) each of which provides methods you can use within the view. For example, in the following view, we use the Html property to access the TextBox method. @Html.TextBox("SomeProperty")
Html is a property of type HtmlHelper and there are a large number of useful extension methods that hang off this type, such as TextBox.
But where did the Html property come from? It’s a property of System.Web.Mvc.WebViewPage, the default base type...
I gave a presentation to another team at Microsoft yesterday on ASP.NET MVC and the Razor view engine and someone asked if there was a reference for the Razor syntax. It turns out, there is a pretty good guide about Razor available, but it’s focused on covering the basics of web programming using Razor and inline pages and not just the Razor syntax. So I thought it might be handy to write up a a really concise quick reference about the Razor syntax. Syntax/Sample ...
UPDATE: Check out my Razor View Syntax Quick Reference for a nice quick reference to Razor.
There’s an old saying, “Good things come to those who wait.” I remember when I first joined the ASP.NET MVC project, I (and many customers) wanted to include a new streamlined custom view engine. Unfortunately at the time, it wasn’t in the card since we had higher priority features to implement. Well the time for a new view engine has finally come as announced by Scott Guthrie in this very detailed blog post. While I’m very excited about the new...