什么是视图引擎?它实际上有什么作用?

发布于 2024-12-18 18:43:26 字数 114 浏览 4 评论 0原文

我开始学习ASP.NET MVC3。

因此,在阅读在线教程和书籍时,我经常遇到这个术语“视图引擎”。我不知道那是什么。

它实际上有什么作用?

为什么它对我来说很重要?

I started learning ASP.NET MVC3.

So, while reading tutorials online and in books, I came across this term "view engine" quite frequently. I don't know what it is.

What does it actually do?

Why should it matter to me at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

栩栩如生 2024-12-25 18:43:26

视图引擎负责从视图创建 HTML。视图通常是 HTML 和编程语言的某种混合。其中大多数背后的模式称为两步视图

例如,ASP.NET 自带了自己的开箱即用的视图引擎。即视图具有大量标签,例如 <% %><%: %>。它使用 .aspx 文件扩展名。

在 ASP.NET MVC3 中,添加了另一个开箱即用的视图引擎,Razor,它具有更吸引人的语法,例如

@Model.UserName

视图引擎的选择很重要,因为视图引擎的功能集有很大不同。例如,有些支持渲染为 PDF 文件;有些不能在 Web 上下文之外使用(对于旧的 ASP.NET 视图引擎来说是这样),而其他则可以(例如 Razor)。当您想要以与构建视图相同的方式创建 HTML 电子邮件并且这些电子邮件应从后台工作人员而不是 Web 应用程序发送时,视图的“离线”渲染会派上用场。

这里有一个关于 asp.net 视图引擎 的很好的比较。

好消息是您可以在 ASP.NET MVC 中并行使用多个视图引擎,但除非必要,否则我不会推荐它。

Razor 引擎中已经有一些非常好的扩展点。例如,您可以提供一个自定义视图基类,这是一个强大的概念,可以轻松地在正确的位置添加大量功能,而不必弄乱您必须处理的所有细节。

我目前会选择 Razor。

The view engine is responsible for creating HTML from your views. Views are usually some kind of mixup of HTML and a programming language. The pattern behind most of these is called two-step view.

For example, ASP.NET comes with its own view engine out of the box. That is the one where views have lots of tags like <% %> and <%: %>. It uses the .aspx file extension.

With ASP.NET MVC3, another out-of-the-box view engine was added, Razor, which has a more appealing syntax, e.g. <div>@Model.UserName</div>.

The choice of view engine is important, because the feature sets of view engines are quite different. Some support rendering to PDF files, for instance; some can't be used outside a web context (this is true for the old ASP.NET view engine), while others can (e.g. Razor). 'Offline' rendering of views comes in handy when you want to create HTML emails the same way you build your views and those emails should be sent from a background worker rather than a web application.

There's a nice comparison of asp.net view engines here on SO.

The good news is that you can use multiple view engines in parallel in ASP.NET MVC, though I wouldn't recommend it unless necessary.

There are some very nice extension points in the Razor engine already. For example, you can provide a custom view base class, a powerful concept that makes it easy to add a lot of functionality in the right place without having to mess with all the gritty details that you'd have to cope with otherwise.

I'd currently go for Razor.

归属感 2024-12-25 18:43:26

视图引擎负责渲染视图,并将代码转换为漂亮的 HTML。因此,他们直接负责您需要如何在视图中编写代码。

基本上有两个您需要关心:ASPX 和 Razor。在我看来,Razor 更时尚、更易于使用,但代价是仅在 MVC3 中受支持。

例如,ASPX 中的代码块可能如下所示:

<% foreach(var item in Model) { %>
    <tr>
        <td><%: item.Name %></td>
    </tr>
<% } %>

而 Razor 中的等效代码块将如下所示:

@foreach(var item in Model) {
    <tr>
        <td>@item.Name</td>
    </tr>
}

The view engine is what's responsible for rendering your view, and converting your code into glorious HTML. As such, they are directly responsible for HOW you need to write code in your views.

There's basically two ones you need to care about: ASPX and Razor. Razor is, in my opinion, much sleeker and easier to use, at the cost of only being supported in MVC3.

For example, a code block in ASPX might look like this:

<% foreach(var item in Model) { %>
    <tr>
        <td><%: item.Name %></td>
    </tr>
<% } %>

Whereas the Razor equivalent will look like this:

@foreach(var item in Model) {
    <tr>
        <td>@item.Name</td>
    </tr>
}
债姬 2024-12-25 18:43:26

在 MVC 中,视图引擎是在视图和浏览器之间工作的引擎,通过编译视图内的代码向浏览器提供有效的 HTML 输出。有许多可用的视图引擎,其中一些如下:

  1. ASPX

  2. Razor

  3. Spark

  4. NHaml

  5. < p>NDJango

  6. Hasic

  7. Brail

  8. Bellevue

  9. 尖锐瓷砖

  10. String Template

  11. Wing Beats

  12. SharpDOM

目前大多数开发者更喜欢使用 Razor 视图引擎,因为它提供了非常方便的编程方式。所有这些视图引擎可能不支持 ASP.NET MVC。

有关更多详细信息,您可以访问本文

In MVC, View engine is the one that works between your View and browser to provide valid HTML output to your browser by compiling the code inside your View. There are many view engines available and some of them are following:

  1. ASPX

  2. Razor

  3. Spark

  4. NHaml

  5. NDJango

  6. Hasic

  7. Brail

  8. Bellevue

  9. Sharp Tiles

  10. String Template

  11. Wing Beats

  12. SharpDOM

Currently most developers prefer to use Razor view engine as it provides very convenient way of programming. All of these view engines may not support ASP.NET MVC.

For more details you can visit this article.

手心的温暖 2024-12-25 18:43:26

MVC 使用视图引擎来查找并呈现您从控制器请求的视图。如果您对默认路由感到满意,您可能不需要更改任何内容,但是假设您希望共享文件通常位于 root/views/shared 中,而不是位于 root/common 中,则您将使用自定义视图引擎需要创造才能做到这一点。

在这里您可以了解如何构建视图引擎:

http://coderjournal。 com/2009/05/creating-your-first-mvc-viewengine/

视图引擎还负责渲染视图,但由于您刚刚学习 MVC,您可能不需要更改渲染功能 然而

A view engine is what MVC uses to find and render the views you are requesting from the controller. If you are satisfied with the default routing you probably wont need to change anything, but lets say you wanted to have your shared files usually located in root/views/shared to instead be located in root/common, a custom viewengine is what you will need to create to be able to do that.

Here you can see how to build a viewengine:

http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/

The view engine is also responsible for rendering the view, but as you are just learning MVC you will probably not need to alter the rendering functionality just yet

煮酒 2024-12-25 18:43:26

模板/视图引擎使您能够在应用程序中使用静态模板文件。在运行时,模板引擎将模板文件中的变量替换为实际值,并将模板转换为发送给客户端的 HTML 文件。这种方法使得 HTML 页面的设计变得更加容易。

A template/view engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.

北方。的韩爷 2024-12-25 18:43:26

View Engine 在应用程序内部工作,用于将 HTML 页面呈现给浏览器或用户。它可以包含 HTML 标签、服务器控件和一些编程语言。

Razor 是 MVC4 框架的首选视图引擎。

View Engine works inside the application for rendering HTML page to the browser or to the user. It can contain HTML tags, server controls and some programming language.

Razor is preferred view engine for MVC4 framework.

风蛊 2024-12-25 18:43:26

在 ASP.Net MVC 中,视图引擎是在视图和浏览器之间工作的引擎,通过考虑视图提供的输出向浏览器提供有效的 HTML 输出。视图引擎有多种类型。

1)ASPX

2)Razor

3)Spark

4)NHaml

5)NDJango

6)Hasic

7)Brail

In ASP.Net MVC, View engine is the one that works between your view and browser to provide valid HTML output to your browser by considering output provided by your view.There are many types of view engines.

1)ASPX

2)Razor

3)Spark

4)NHaml

5)NDJango

6)Hasic

7)Brail

_蜘蛛 2024-12-25 18:43:26

我在

视图引擎能够将 HTML 从视图渲染到浏览器。

ASP.NET MVC 支持许多视图引擎,但使用最广泛的视图引擎是

  • Web 表单/ASPX 视图引擎。
  • 剃刀视图引擎。

Web表单视图引擎/ASPX视图引擎:

  • Web表单视图引擎/ASPX视图引擎是默认视图引擎
    对于 Asp.net MVC 项目。它从 MVC 1.0 开始提供
  • 。Web 表单引擎的命名空间是 Web.Mvc.WebFormViewEngine
  • 此视图引擎的文件扩展名与 Web 表单类似:

.aspx,对于视图,就像 Web 表单页面一样。
.ascx,用于部分视图和编辑器模板就像用户控件一样。
.master,用于布局和母版页,就像 Web 表单中的母版页一样。

  • 不支持 TDD(测试驱动开发)。
  • Web Form Engine 不能防止 XSS 攻击,这意味着任何脚本都会被保存
    渲染页面时将触发数据库中的
  • 语法: <%: Html.ActionLink(“Home”, “Index”) %>

Razor 视图引擎

  • Razor 视图引擎是一种高级视图引擎,可与 MVC 一起使用
    3.0 及更高版本
  • ASPX 视图引擎的命名空间是 Web.Razor。
  • 此视图引擎的文件扩展名是 .cshtml (Razor C#),对于视图,
    部分视图、编辑器模板和布局页面。 .vbhtml(剃刀
    VB.NET),用于视图、部分视图、编辑器模板和布局页面。
  • 支持TDD(测试驱动开发)。
  • 与 Web 表单引擎相比,Razor 引擎有点慢。
  • Razor引擎防止XSS攻击(跨站脚本攻击)的手段
    它对脚本或 html 标签进行编码,例如 <,>在渲染视图之前。
  • Razor 语法很容易理解,而且比 Web Form 简洁得多
    句法。 Razor 使用 @ 符号使代码如下:

    @Html.ActionLink(“Home”, “Index”)

I read a descriptive post at http://questionbox.in/view-engine-asp-net-mvc-razor-view-engine-asp-net-mvc-web-form-aspx-view-engine-asp-net-mvc/

View engine gives the ability to render the HTML from your view to the browser.

There are many view engines supported by ASP.NET MVC but the most widely used view engines are

  • Web form / ASPX view engine.
  • Razor view engine.

Web form view engine / ASPX view engine:

  • Web Form View Engine / ASPX View Engine is the default view engine
    for the Asp.net MVC project. It is available from MVC 1.0
  • The namespace for Web Form Engine is Web.Mvc.WebFormViewEngine
  • File Extension for this View Engine is similar to Web Form as:

.aspx, for Views just like Web Form pages.
.ascx, for Partial Views & Editor Template just like User Controls.
.master, for Layout and Master Pages just like Master Pages in Web Forms.

  • No support for TDD (Test Driven Development).
  • Web Form Engine does not prevent XSS attacks means any script saved
    in the database will be fired while rendering the page
  • Syntax : <%: Html.ActionLink(“Home”, “Index”) %>

Razor View Engine:

  • The Razor View Engine is an advanced view engine, available with MVC
    3.0 and later versions
  • Namespace for ASPX view Engine is Web.Razor.
  • File Extension for this View Engine is .cshtml (Razor C#), for Views,
    Partial Views, Editor Template and Layout Pages. .vbhtml (Razor
    VB.NET), for Views, Partial Views, Editor Template and Layout Pages.
  • Supports TDD (Test Driven Development).
  • Razor Engine is little bit slow as compared to Web form Engine.
  • Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means
    it encodes the script or html tags like <,> before rendering to view.
  • Razor syntax is easy to understand and much clean than Web Form
    syntax. Razor uses @ symbol to make the code like as:

    @Html.ActionLink(“Home”, “Index”)

浅笑轻吟梦一曲 2024-12-25 18:43:26

视图引擎将视图呈现为 HTML 形式给浏览器。如果我们谈论 .Net Framework 中的 MVC 应用程序,它支持以下 2 个视图引擎:

1。 Razor 视图引擎
2. Web 表单/ASPX 视图引擎

差异:
1. Razor 视图引擎使用布局,但 ASPX 视图引擎使用母版页。

2. Razor 视图引擎使用部分页面,但 ASPX 视图引擎使用 Web 用户控件。

3. Razor视图引擎不是一种语言,它是标记语法。

4. @' 符号在 Razor 引擎中使用来编写代码。 @Html.ActionLink("登录", "LoginView")
'<%:' 分隔符用作起始点,'%>'用作终点。您可以在 ASPX Engine 中编写它们之间的代码。

5. Razor View Engine 具有用于视图、布局和部分视图的 .cshtml(使用 C#)和 .vbhtml(使用 VB)扩展名。
ASPX 视图引擎具有与简单 Web 应用程序类似的扩展名,例如用于视图的 .aspx、用于用户控件的 .acsx 以及用于母版页的 .master。

View Engine renders the view into HTML form to the browser. If we talk about an MVC application in the .Net Framework, it supports the following 2 view engines:

1. Razor View Engine
2. Web Form/ASPX View Engine

Differences :
1. Razor View Engine uses Layouts but ASPX view engine uses Master pages.

2. Razor View Engine uses partial page but ASPX view engine uses Web User Control.

3. Razor view engine is not a language, It is Markup syntax.

4. @’ symbol uses in Razor Engine to write the code. @Html.ActionLink("Login", "LoginView")
‘<%:’ delimiters use as starting point and ‘ %>’ use as ending point. You can write the code between them in ASPX Engine.

5. Razor View Engine has .cshtml (with C#) and .vbhtml (with VB) extension for views, Layout and Partial views.
ASPX View Engine has a similar extension as in a simple web application like .aspx for the views, .acsx for UserControls and .master for Master Pages.

凡尘雨 2024-12-25 18:43:26

到目前为止,每个回复都与 ASP.NET 有关。如果您正在寻找 Nodejs 的视图引擎,那么以下一些是选项:

Jade (Pug)
express-handlebars
ejs
React

我在 https://github.com/expressjs/express/wiki#template-engines 但上面的似乎是我听到最多的。

So far every response is related to ASP.NET. If you are looking for a view engine for nodejs, then some of the following are options:

Jade (Pug)
express-handlebars
ejs
React

There's a whole list of them that I found at https://github.com/expressjs/express/wiki#template-engines but the above ones seem to be the ones I hear about the most.

野心澎湃 2024-12-25 18:43:26

两个视图引擎:

ASPX

ASPX视图引擎,服务器端脚本包裹在[% %]之间
.ASPX 是它的扩展名。

剃刀

剃刀我们使用@。
HTML 和代码之间的切换是可能的。
Razor 视图扩展名 .CSHTML 和 .VBHTML

Two View Engines:

ASPX

ASPX view engine, the server side script is wrapped between [% %]
.ASPX is its extention.

Razor

Razor we use @.
Switching between HTML and code is Possible.
Razor View extention .CSHTML and .VBHTML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文