What is view engine? What does it actually do? - c#

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.

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>
}

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

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:
ASPX
Razor
Spark
NHaml
NDJango
Hasic
Brail
Bellevue
Sharp Tiles
String Template
Wing Beats
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.

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.

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.

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

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

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.

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.

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

Related

How Can I use HTML Helper in asp.net? [duplicate]

I'm liking the Razor syntax that Microsoft has developed for inline coding in their WebMatrix product (http://en.wikipedia.org/wiki/Microsoft_WebMatrix).
Now that Visual Studio SP1 has RTM'd, is it possible (and/or planned) to enable the use of Razor syntax in ASP.NET Webforms?
We (the ASP.NET team) currently have no plans to support the WebForms page model using Razor syntax. Furthermore it is unlikely that we would ever seriously consider this as the models are too different to make the two work together.
You can use Razor pages without MVC; this is called ASP.Net WebPages.
Just add .CSHTML files to a normal ASP.Net 4.0 project.
I explained how this works in my blog.
You could possibly integrate it using the RazorEngine available from Codeplex. It allows you to process razor outside of MVC. Though you don't get all the features you get from the MVC version of Razor, such as #Html and #Url and others.
I dare say that Microsoft have considered it, but there is no official word on the subject. Since you are not supposed to have C# or VB code in the ASPX file, you have to wonder about the point of adding Razor support to Web Forms. The code behind would still be a (partial) class file. You wouldn't put Razor there any more than you would put it in a class file in Web Pages or MVC. And swapping Server Controls and all that good declarative stuff for Html Helpers removes one of the key reasons for going the Web Forms route, IMO.
This really isn't that difficult to do. Working on it right now. Grab RazorEngine from CodePlex. It lets you compile Razor cshtml/vbhtml files into a class at runtime.
You can then take that class, and use it from a ASP.Net server control, inside its Render method. This is a great way to get HTML out of the body of a server control. Much, much cleaner.
Now, you can also add some methods that you can invoke from Razor. For instance, add something like RenderChild. Additionally, you could set the Server Control itself as the Model available to Razor. You could then invoke into the server control. Including doing something like grabbing one of it's child controls and invoking Render.

Coverting SPA template to use razor instead of html view

I am interested into looking at SPA's with MVC. I realized I can use Kendo UI controls with this as well, which is great.
So I am following a post at http://a.shinynew.me/post/78038782111/a-kendo-ui-asp-net-mvc-spa-template which is interesting.
The issue is that I am experienced with mvc and razor, and it looks like this template takes the html route, which seems like a bit of a steeper learning curve, and won't be as easy to leverage my existing code. I can't figure out how to hook up new views to the MVC methods, everything hits the Home / Index method.
Then I read [link in comments because I need 10 rep to post 2 links] but the template that you download doesn't seem to have anything under Script/App or App. The project seems completely different now, irritatingly. But then, I noticed this:
You have two options for SPA views that will be loaded into the SPA:
standard HTML (.html) or ASP.NET MVC Razor (.cshtml) pages. Because
this article is intended for .NET developers—and a lot of enterprises
have server-side libraries and frameworks they’d like to continue
using in their views—I’ll go with the latter option of creating Razor
views.
So my question is, how could I convert the http://a.shinynew.me/post/78038782111/a-kendo-ui-asp-net-mvc-spa-template template to use Razor views instead of .html

Using Razor and Spark simultaineously in ASP.NET MVC 2 project

How to use certain view engine for certain controller?
Now my project using MVC2 + Spark view engine. I want to migrate to Razor view engine. Project so big, and I want to do this step-by-step.
It's perfectly possible to run both view engines simultaneously. Since you're currently using Spark you probably have an entry in your global.asax that clears the current view engines and adds in the Spark engine. Something like:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ...);
MVC is designed to use FindView and FindPartial (part of the ViewEngine interface) to find the correct view for an action, and if it can't find one for the first view engine in the list, it moves on to the next viewengine and looks for its appropriate views, until there aren't any other view engines to try.
It's up to you whether it looks for Razor Views first and then falls back to Spark, or the other way round, so make sure you place the ViewEngines in the correct order. Or to have the default engines, you can delete the ViewEngines.EnginesClear() line and make Spark first by using ViewEngines.Engines.Insert(0, ...)
If that's not clear then paste in the global.asax code around the Spark bit and I can try to help you more.

Converting MVC2 to MVC3 and pages to Razor

First and foremost reason to update big old MVC2 project for me is the Razor engine. Now following this instructions
I successfully converted the project without yet touching .aspx and .ascx files.
1) Can you guys tell me now, how can I convert Master pages to layouts, and start converting pages and user controls without breaking any existing MVC2 pages? (it's a large project, can't do them all at once). Can I somehow use old master pages as layouts in razor pages?
I converted a couple of partial aspx pages which don't use a master page.
I used Telerik's aspx2razor converter - it helps although you still have to do a lot of manual revisions.
2) Is there any other better tool out there?
You can use Razor and the normal views side by side. By default, MVC3 looks for razor first, then will fall back to the WebFormsViewEngine.
So you should have no problem, and you can tackle each page in time as you see fit.

Move baseclasses for MasterPage from MVC2 to MVC3

I am working on moving an application from MVC2 ASPX to MVC3 Razor, and is quite stuck moving a baseclass for more MasterPages in old MVC2 application.
The baseclass is used for automate include of css and js on pages in order to ease quickfix and debugging when developing application in local environment, but when running application in production environment it has to update and include single minimized css and js files delivered from a external CDN.
The code needs to know about the View file eg. "~/views/home/index.chtml" and/or the Layout file eg. "~/Views/DefaultNoLogon.Master" in order to include and handle css and js files correct.
I tried to implement own baseclass using the pageBaseType in Razor part of web.config, but it seems like it is executed both for View and Layout file, and I could not find a execution point where information about both View and Layout file is present. I also tried to implement the file logic using a HtmlHelper, but I can only access information about the View file and miss information for Layout file for View.
I don't want this kind of code to be implemented in Route, Controller or ViewModel since it should be related directly to generation of Views.
Any ideas how to get information about View and Layout files in MVC3 Razor app?
Well, never mind my question.
I redesigned my logic for automatic rendering of js and css files, which actually is more simple and works better. My Mvc3 Razor app is now capable to take all js and css files for a view (including css and js for all layoutpages) and render them into single minified files.
Works like a charm, and I guess it's more future proof than my old solution, unless Guthrie make fundamental changes in layoutpages and Html helpers in Mvc4 or later.

Categories

Resources