asp.net-mvc Razor 语法是否在 UI 标记中提供了引人注目的优势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4019740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:32:42  来源:igfitidea点击:

Does Razor syntax provide a compelling advantage in UI markup?

asp.net-mvcsyntaxmarkuprazorasp.net-mvc-3

提问by Phil.Wheeler

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

我注意到斯科特·格思里已经开始提剃须刀一个公平位在他的博客,但我只是不那么肯定,这是一个很适合我的风格。

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.

诚然,对于已经习惯了“标准”类型的 ASP.Net 标记(内容占位符和内联代码)的人来说,这是一种相当陌生的风格,但感觉就像有很多额外的页面需要管理,而对我来说标记不太清楚。

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 页面时应该认真考虑它,还是只是试图解决一个不存在的问题?

回答by marcind

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

[免责声明:我是 MVC 和 Razor 的 Microsoft 开发人员之一,所以我可能有点偏见:)]

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.

我们将 Razor 设计为一种简洁的模板语言,只使用最少的必要控制字符。我想说的是,与使用“传统”WebForms 语法的相同代码相比,您的大部分视图可以用更少的字符来表达。

For example the following code snippet in ASPX syntax:

例如以下 ASPX 语法中的代码片段:

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

Can be expressed as follows in Razor:

可以用Razor表示如下:

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

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

ASPX 版本有 21 个过渡字符(<%%>),而 Razor 版本只有三个(@

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

我想说 Razor 的优点如下:

  1. Concise syntax, which is very similar to the way you write regular C# code (check out the following recent blog post by Phil Haack comparing Asxp with Razor syntax: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx)
  2. Automatic HTML encoding of output (which helps protect you from html injection attacks)
  3. Built in (though not 100%) validation of your markup which helps you avoid unbalanced tags
  1. 简洁的语法,这与您编写常规 C# 代码的方式非常相似(请查看 Phil Haack 最近的博客文章,将 Asxp 与 Razor 语法进行了比较:http://haacked.com/archive/2011/01/06/razor-语法快速参考.aspx)
  2. 输出的自动 HTML 编码(这有助于保护您免受 html 注入攻击)
  3. 内置(虽然不是 100%)标记验证,可帮助您避免不平衡的标签

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

与页面相关的概念也很容易从您在 ASPX 中拥有的内容映射出来

  • 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>
  • 如您所见,仍然允许使用内联代码
  • 部分(可以是可选的)相当于内容占位符
  • 布局页面而不是母版页面
  • 全视图和局部视图的概念是一样的
  • @functions { ... }块而不是 <script runat="server"> ... </script>

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

此外,Razor 有许多有用的概念,我认为它们比 ASPX 中可用的概念要好:

  • @helperfunctions for really easy creation of functions that emit markup
  • @modelkeyword for specifying your view's model type without having to write a <%@ Page ...directive with the full class name
  • @helper用于真正轻松创建发出标记的函数的函数
  • @model用于指定视图模型类型的关键字,而无需编写<%@ Page ...具有完整类名的指令

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.

我想我们已经解决了一个真正的问题,即让您更轻松地编写简洁且符合标准的视图,同时为您提供重构公共代码的方法。

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

当然,并不是每个人都会喜欢这种语法,这也是我们完全支持 ASPX 视图引擎的原因。此外,您可以查看 Spark 和 NHaml,它们是两个享有重要社区关注度的 3rd 方视图引擎。以下博客文章对不同产品进行了很好的比较:http: //blogs.msdn.com/b/coding4fun/archive/2010/10/04/10070953.aspx

回答by Travis J

Personally I really appreciate the reduction in how many escape characters are used. Using <% %>gets very tedious when compared to @{}and is not nearly as syntactically appealing.

就个人而言,我非常感谢减少使用转义字符的数量。<% %>相比之下,使用变得非常乏味,@{}并且在语法上几乎没有吸引力。

Moreover, writing a whole definition for the codebehind and page is simplified to a single @model model.

此外,为代码隐藏和页面编写整个定义被简化为单个@model model.

As also noted by marcind, not having to always include runat=serveris very nice also.

正如 marcind 所指出的,不必总是包含runat=server也很好。

Overall, I really appreciate using the Razor engine and find it not only makes things easier on me to develop but also makes code easier to read.

总的来说,我真的很欣赏使用 Razor 引擎,并且发现它不仅让我的开发变得更容易,而且让代码更易于阅读。