`
netatomy
  • 浏览: 44066 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

What's new in razor v2

阅读更多

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,nullwas 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”>

IfisCheckedistrue, we renderchecked=”checked”, if it’sfalse, we don’t render thecheckedattribute at all.

Finally, we do NOT treatString.Empty("") likenullin 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 betweennullandString.Empty. Null indicates the complete absence of a value, whereasString.Emptyis 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/

分享到:
评论

相关推荐

    jqGrid in ASP.NET MVC 3 and Razor.zip

    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 Razor原版文档

    Razor原版文档,讲的挺好,英文版,有助于理解.Net 中 MVC Razor引擎

    Vipul's Razor-开源

    Vipul的Razor是一个分布式的协作式垃圾邮件检测和过滤网络。 该系统的主要重点是在垃圾邮件的注入和处理完成之前识别并禁用它。

    Razor-1.6.0.57_razor_ultimaonline_UO_

    Razor connector

    脱离mvc环境使用razor模板引擎

    这个是我本人写的《在winform使用razor模板引擎》的一个例子,参考了msdn上面的文章(作者:Matt Wrock,文章名称:Using the Razor templating engine outside of MVC)及某篇在winform上面配置razor 智能提示的文章...

    在非网页程序里使用razor模板引擎

    这是本人翻译的一篇英文文章【Hosting the Razor Engine for Templating in Non-Web Applications】里面的例子,可以直接使用,有兴趣可以看我的译文版【假如你不嫌弃翻译渣的话】,也可以直接查看原文地址:...

    Razor视图引擎语法

    Razor视图引擎语法 注意事项 等等

    MVC3.0 中Razor 学习

    Razor出现后我们就可以选择不再使用asp.net master 模板页。取而代之的是cshtml razor的模板文件。

    Razor Demo

    一个 Razor模板解析的例子,包括了解析Razor模板,传入dynamic 类型的Model,以及模板的执行

    Programming Razor 无水印pdf

    Programming Razor 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    javascript模版工具kino.razor.zip

    kino.razor-一个javascript模版工具。简单易用,而且入门成本极低,对于熟悉asp.net mvc的razor模版的程序员更是0学习成本。 如何使用? web页面使用时,只需要增加script标签引入: ...

    razor语法手册

    razor开发必备的语法,有简单的实例应用,对于刚接触razor的朋友很有帮助。

    Go-Golang的Razor视图引擎

    Golang的Razor视图引擎

    Razor的语法

    Razor的语法,Razor的语法,Razor的语法

    razor-converter-master

    The razor-converter is a simple tool for converting Microsoft® ASP.NET MVC WebForms Views to the new Razor syntax. It was initially developed by the Telerik ASP.NET MVC team for internal use, but it ...

    Exploring Advanced Features in C#-.pdf

    • See what is new in .NET Core 3.0 and how to get up and running with .NET Core 3.0 in a Snap • Have a look at running your ASP.NET Core MVC app on Linux and how to edit and debug it using Visual ...

    Razor语法的示例程序

    Razor语法的示例程序包括页面生成 数据库使用 邮件使用 一用俱全

    aspx2razor

    MVC3,aspx2razor可以方便将ASPX转换成RAZOR视图的文件,便于MVC3开发.

    .NETMVC @Razor

    增删改查 登录功能 注册功能 新手上路必备 此项目有后台

    razor引擎独立使用的一个项目

    网上找的把razor引擎独立使用的一个项目

Global site tag (gtag.js) - Google Analytics