什么是视图引擎?它实际上是做什么的? [英] What is view engine? What does it actually do?

查看:467
本文介绍了什么是视图引擎?它实际上是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习ASP.NET MVC3.

因此,在在线和在书上阅读教程时,我经常碰到这个术语视图引擎" .我不知道那是什么

它实际上是做什么的?

为什么对我完全重要?

解决方案

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

例如,ASP.NET附带了自己的视图引擎.那是其中视图具有很多标记(例如<% %><%: %>)的视图.它使用.aspx文件扩展名.

使用ASP.NET MVC3,添加了另一个现成的视图引擎,剃刀,它具有更吸引人的语法,例如<div>@Model.UserName</div>.

视图引擎的选择很重要,因为视图引擎的功能集完全不同.例如,有些支持渲染到PDF文件.有些不能在Web上下文之外使用(对于旧的ASP.NET视图引擎而言是正确的),而另一些则不能使用(例如Razor).当您要以与构建视图相同的方式创建HTML电子邮件时,视图的脱机"呈现非常方便,并且这些电子邮件应从后台工作人员而不是从Web应用程序发送.

在SO上有一个很好的比较 asp.net视图引擎

好消息是,您可以在ASP.NET MVC中并行使用多个视图引擎,尽管除非必要,否则我不建议这样做.

Razor引擎中已经有一些非常不错的扩展点.例如,您可以提供一个自定义视图基类,这个强大的概念使您可以轻松地在适当的位置添加很多功能,而不必弄乱您必须应对的所有棘手细节. >

我现在要去剃刀.

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?

解决方案

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.

这篇关于什么是视图引擎?它实际上是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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