I know, it’s been too long since I blogged, and I won’t even bother saying I’ll try to blog more (though I will) because hopefully you’ll see that for yourself. A lot has happened since I last blogged but the biggest thing is that the ASP.Net Web
Stack (which is what we call the out-of-band stuff we ship, like MVC, WebPages, WebAPI and Razor) is now truly an open-source project! We’re hosted on CodePlex (http://aspnetwebstack.codeplex.com/)
and if you want more details, check out Scott Hanselman’s awesomepostabout
it.
Now that we’re open, I’ll be able to start talking a little more directly about what we’re doing. It’s very exciting for us, and to alleviate your worries that open-source means Microsoft isn’t working on it anymore, we’re still working full-time
on our current release and even starting a bit of planning for v.Next.
All that aside, we also released a new version of Razor since I’ve blogged and I thought I’d give you a quick run-through of the features. In later posts, I’ll go over the details of what’s changed as well as some new information for those hosting
Razor outside of ASP.Net on how to take advantage of those features.
~/ – url resolution made easy, goodbye @href/@url.content!
One of the most common patterns in MVC Views is this:
<script src=”@Url.Content(“~/Scripts/myscript.js”)”></script>
Well, in Razor v2, you can express the same intent with much less code, and it looks much cleaner too:
<script src=”~/Scripts/myscript.js”></script>
Note the lack of any code transitions! How does this work? It’s quite a simple algorithm: Whenever we see an attribute value that starts “~/”, we treat it like a URL and replace it with a call to@Href
(in
WebPages) or@Url.Content
(in MVC). Note that this is ANY attribute, so if you typed the following:
<div class=”~/Foo”>
We’ll treat “~/Foo” as a URL. We made this choice because we didn’t want to limit you to a list of attributes that we think have URLs. Especially when you might want to put URLs in adata-
attribute.
Imagine using adata-
attribute to tell your client-side javascript what the root of your application is:
<html data-root=”~/”>
Then you can use jQuery to access this data:$(document).data(‘root’)
and use it when making
Ajax calls to make sure your app is portable even if it’s in a sub-folder.
What if you want to inject code snippets in to your URL? Well that’s easy, just treat it like you would if you were injecting code in to other attribute values:
<a href=”~/MySite/@Foo/Bar/@Baz”>Something!</a>
I should note that we’re actually not doing much special here, the code above is equivalent to the following MVC code in Razor v1:
<a href=”@Url.Content(“~/MySite/”)@Foo/Bar/@Baz”>Something!</a>
So we are just resolving that first portion of the URL and then going back to regular string concatenation for the rest.
conditional attributes
The other major feature (there are a few others I’ll go over in later posts but they are smaller) is Conditional Attributes. I’ll freely admit we borrowed this feature heavily from the fantasticSpark
View Enginewritten by our very ownLouis Dejardins.
Have you ever typed code like this in Razor v1?
@{ var cls = GetClass(); }
<div id=”foo”@if(cls != null) { <text>class = “@cls”</text> }>
If not, let me explain why you’d want to do this. In Razor v1,null
was treated the same as
an empty string so if you were to have code like this:
<div id=”foo” class=”@cls”>
Then if cls was null, we’d render
<div id=”foo” class=””>
Eugh! That looks ugly! In Razor v2, the same code would render this:
<div id=”foo”>
Note the missing class attribute? We’ve even taken away the leading space! Another feature of this is that we’ll also collapse whitespace within the attribute value:
@{ string foo = null; string bar = “bar” }
<div id=”foo” class=”@foo @bar”>
Becomes:
<div id=”foo” class=”bar”>
We also special case boolean values. If the expression evaluates tofalse
, we treat it the same asnull
.
If it evaluates totrue
, we render out the attribute name again. This allows you to write code like the following for
checkboxes:
<input type=”checkbox” checked=”@isChecked”>
IfisChecked
istrue
,
we renderchecked=”checked”
, if it’sfalse
,
we don’t render thechecked
attribute at all.
Finally, we do NOT treatString.Empty
("") likenull
in
this case. If the expression evaluates to an empty string, we WILL render the attribute:
@{ var foo = ""; }
<div class=”@foo”>
Renders:
<div class=””>
The reason for this lies in the difference betweennull
andString.Empty
.
Null indicates the complete absence of a value, whereasString.Empty
is a value, a string of length 0.
In the currently released (Beta) version of Razor, we do this for all attributes. However, in the next release (and, in fact, in the live code on CodePlex) we have entirely disabled the conditional attributes feature fordata-
attributes.
You'll still get the ~/ URL resolution, but we won't remove null values or do fancy tricks with booleans indata-
attributes.
This was done because those attributes are usually read from JavaScript, so the semantics are different and we wanted to give you full control.
there’s much more!
That’s just a quick summary. I’ll be publishing another post soon with even more new features. Then, we’ll go in to how Conditional Attributes is implemented and how you can make sure your custom Razor hosts support this feature.
原文地址:http://vibrantcode.com/blog/2012/4/10/whats-new-in-razor-v2.html/
分享到:
相关推荐
在"Layouts in Razor"部分中,文档讲解了如何在Razor模板中使用布局来提高代码的重用性。布局允许开发者创建一个具有预定义内容的页面框架,然后在各个页面中重用这个框架,仅需修改框架内的特定部分。内容占位符、...
jqGrid in ASP.NET MVC 3 and Razor.zipjqGrid in ASP.NET MVC 3 and Razor.zipjqGrid in ASP.NET MVC 3 and Razor.zip
标题 "Razor-1.6.0.57_razor_ultimaonline_UO_" 指向的是一个特定版本的Razor软件,这是专为Ultima Online(UO)游戏设计的一个辅助工具。Razor是一个流行的游戏客户端增强器,它提供了许多功能,如聊天增强、自动...
@foreach (var user in users) { <li>@user } ``` #### 四、条件语句 - **作用**:根据条件执行不同的代码块。 - **示例**: ```razor @{ bool isAdult = true; } @if (isAdult) { 成年人 } else ...
Vipul的Razor是一个分布式的协作式垃圾邮件检测和过滤网络。 该系统的主要重点是在垃圾邮件的注入和处理完成之前识别并禁用它。
Razor是一种轻量级的视图引擎,广泛用于ASP.NET MVC和ASP.NET Core框架中,用于构建动态网页和Web应用程序。这个"Razor Demo"是一个实际应用案例,它展示了如何利用Razor模板来生成HTML输出,并与动态类型的Model...
在这个".NET MVC @Razor"项目中,我们将会探讨如何利用这些技术来实现常见的Web应用功能,如增删改查、登录和注册等。 **增删改查(CRUD)操作** 在Web开发中,CRUD(Create, Read, Update, Delete)是基本的数据...
这个是我本人写的《在winform使用razor模板引擎》的一个例子,参考了msdn上面的文章(作者:Matt Wrock,文章名称:Using the Razor templating engine outside of MVC)及某篇在winform上面配置razor 智能提示的文章...
Razor视图引擎的出现极大地简化了视图模板的编写,使得开发者能够更加专注于业务逻辑和数据呈现。 在ASP.NET MVC3中,Razor视图引擎成为了默认的视图引擎,取代了之前的ASPX视图引擎。Razor语法的核心特点是使用`@`...
这是本人翻译的一篇英文文章【Hosting the Razor Engine for Templating in Non-Web Applications】里面的例子,可以直接使用,有兴趣可以看我的译文版【假如你不嫌弃翻译渣的话】,也可以直接查看原文地址:...
在ASP.NET MVC或Web应用程序开发中,Razor视图引擎是一种强大的工具,它允许开发者用C#或VB.NET语法编写动态网页。本示例“razor table分页、隔行变色 table_pages_color_demo”专注于如何在网页上展示数据,同时...
Razor语法是微软开发的一种轻量级视图引擎,主要用于ASP.NET MVC和ASP.NET Core框架,用于创建动态网页内容。Razor以其简洁、直观的语法深受开发者喜爱,它将HTML、C#或VB.NET代码无缝融合,使得视图层的编写更加...
这个项目提供了一种方式,使得Razor引擎可以脱离ASP.NET MVC或Web Pages框架独立使用,这对于需要在非Web环境中或者自定义应用程序中生成动态内容的开发者来说非常有用。 Razor引擎的核心特点在于其简洁的语法,它...
Go-Golang的Razor视图引擎是Go语言中一种基于语法类似于ASP.NET Razor的模板引擎,它为Go Web开发提供了一种高效的视图渲染解决方案。Razor视图引擎以其简洁、直观的语法特性,使得开发者可以更加专注于业务逻辑,而...
Razor出现后我们就可以选择不再使用asp.net master 模板页。取而代之的是cshtml razor的模板文件。
有两种主要的视图引擎:ASPX和Razor。 【Razor视图】 Razor视图引擎是ASP.NET MVC 3引入的一种新语法,其设计目标是提供更简洁、更直观的代码编写方式。与ASPX视图相比,Razor视图的语法更紧凑,更接近于纯HTML,这...
var result =Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" }); 这里提供编译好了的dll,源码可以去官方托管去下,很多朋友下载后编译不通过,所以这里提供dll。
ASPX2Razor是一个工具,专门设计用于帮助开发者将传统的ASP.NET Web Forms视图(.aspx文件)转换为ASP.NET MVC的Razor视图(.cshtml或.vbhtml文件)。这个工具对于那些希望从ASP.NET Web Forms迁移至ASP.NET MVC框架...
通常,我们会在ASP.NET MVC或ASP.NET Core MVC项目中使用Razor,但在非MVC环境下使用Razor可能需要一些特殊的策略和技术。 在非MVC环境下使用Razor引擎,首先需要理解的是,MVC(Model-View-Controller)模式是一种...
kino.razor-一个javascript模版工具。简单易用,而且入门成本极低,对于熟悉asp.net mvc的razor模版的程序员更是0学习成本。 如何使用? web页面使用时,只需要增加script标签引入: ...