What is view engine? What does it actually do?

2020-02-07 19:44发布

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?

10条回答
一纸荒年 Trace。
2楼-- · 2020-02-07 20:00

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.

查看更多
Bombasti
3楼-- · 2020-02-07 20:01

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

查看更多
Root(大扎)
4楼-- · 2020-02-07 20:02

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

查看更多
淡お忘
5楼-- · 2020-02-07 20:06

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.

查看更多
再贱就再见
6楼-- · 2020-02-07 20:07

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>
}
查看更多
太酷不给撩
7楼-- · 2020-02-07 20:12

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”)

查看更多
登录 后发表回答