是否剃刀语法提供UI标记一个令人信服的优势在哪里? [英] Does Razor syntax provide a compelling advantage in UI markup?

查看:190
本文介绍了是否剃刀语法提供UI标记一个令人信服的优势在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到斯科特格思里已经开始提剃刀一个< A HREF =htt​​p://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx>在他的博客,但我公平一点只是没有那么肯定,这是一个非常适合我的风格。

I notice Scott Guthrie is starting to mention Razor a fair bit on his blog but I'm just not that sure that it's a good fit for my style.

当然它是一个人一个相当陌生的风格谁是用于标准之类的ASP.Net标记(内容占位符和内联code)条pretty,但它只是感觉就像很多其他页面管理不清晰的标记给我。

Granted it's a fairly unfamiliar style for someone who's pretty used to a "standard" sort of ASP.Net markup (content place holders and inline code), but it just feels like a lot of additional pages to manage and less clear markup to me.

什么是别人的就可以了感情?难道你认为应该脚手架新的MVC页面时,必须认真考虑,或只是试图解决一个不存在的问题?什么

What are other peoples' feelings on it? Is it something that you believe should be seriously considered when scaffolding new MVC pages or is it just trying to solve a problem that doesn't exist?

推荐答案

[免责声明:我对MVC和剃刀微软开发商之一,所以我可能会有点偏颇:)]

[Disclaimer: I'm one of the Microsoft developers on MVC and Razor, so I might be a bit biased :)]

我们设计的剃须刀是只使用控制字符的最小必要量的简明模板语言。我会说你的意见,大型零件可配用的传统的WebForms语法相同code字少pssed前$ P $。

We designed Razor to be a concise templating language that uses only the minimal necessary amount of control characters. I would say that large parts of your views can be expressed with fewer characters than the same code using the "traditional" WebForms syntax.

例如下面的code在ASPX语法片段:

For example the following code snippet in ASPX syntax:

<% if(someCondition) { %>
  <ol>
  <% foreach(var item in Model) { %>
     <li><%: item.ToString() %></li>
  <% } %>
  </ol>
<% } %>

可pssed前$ P $在剃刀如下:

Can be expressed as follows in Razor:

@if(someCondition) {
   <ol>
   @foreach(var item in Model) {
      <li>@item.ToString()</li>
   }
   </ol>
}

虽然ASPX版本的有21个过渡字符(&LT;%%&GT; ),剃刀版本只有三个( @

While the ASPX version has 21 transition characters (the <% and %>), the Razor version has only three (@)

我要说的是剃刀的优点如下:

I would say that the advantages of Razor are as follows:


  1. 简明语法,这是非常相似的,你写的常规C#code(检查出由菲尔哈克以下最近的一篇博客剃刀语法比较Asxp方式:<一href=\"http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx\">http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx)

  2. 输出的自动HTML编码(这有助于保护您免受HTML注入攻击)

  3. 内置在你的标记(虽然不是100%)的验证,它可以帮助您避免不平衡的标记

页面相关的概念也很容易从你的ASPX什么地图

The page-related concepts also map easily from what you have in ASPX


  • 正如你所看到内嵌code仍然是允许

  • 段(可选配)等同于内容占位符

  • 布局页,而不是母版页

  • 的全部或部分视图的概念是相同

  • @functions {...} 块而不是&LT;脚本=服务器&GT; ...&LT; / SCRIPT&GT;

  • As you can see inline code is still allowed
  • Sections (which can be optional) are equivalent to content placeholders
  • Layout pages instead of Master pages
  • The concepts of full and partial views are the same
  • @functions { ... } blocks instead of <script runat="server"> ... </script>

此外剃刀有许多有用的概念,我会说是比什么ASPX提供更好的:

In addition Razor has a number of useful concepts that I would say are better than what is available in ASPX:


    为真正轻松创建能发出标记功能
  • @helper 功能

  • %@页... @model 指定您的视图的机型,而无需编写&LT关键字>指令与类的全名
  • @helper functions for really easy creation of functions that emit markup
  • @model keyword for specifying your view's model type without having to write a <%@ Page ... directive with the full class name

我想认为,我们所要解决的一个现实问题,这是为了让你更轻松地编写简洁,符合标准的意见,而在同一时间为您提供的方式来重构共同code。

I would like to think that we have tackled a real problem, which is to allow you to more easily write concise and standards-compliant views while at the same time providing you with ways to refactor common code.

当然,不是每个人都会preFER这就是为什么我们也完全支持ASPX视图引擎的语法。此外,您还可以检查出Spark和NHaml,这是两个第三方视图引擎,享受显著社区如下。下面的博客文章有不同的产品的一个很好的比较:<一href=\"http://blogs.msdn.com/b/coding4fun/archive/2010/10/04/10070953.aspx\">http://blogs.msdn.com/b/coding4fun/archive/2010/10/04/10070953.aspx

Of course, not everyone will prefer the syntax which is why we are also fully supporting the ASPX view engine. In addition you can check out Spark and NHaml, which are two 3rd-party view engines that enjoy significant community following. The following blog post has a good comparison of the different offerings: http://blogs.msdn.com/b/coding4fun/archive/2010/10/04/10070953.aspx

这篇关于是否剃刀语法提供UI标记一个令人信服的优势在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆