0

As we know, MVC in .Net supports two view engines, namely, ASPX and Razor. I have read several articels and have observed that Razor is unanimously(almost) considered as a better option between the two. The reasons being:

  • Razor is clean - Agreed. The syntax itself makes it cleaner.
  • Razor syntax is simple - Agreed. There is no need to close the tags.
  • Razor is light-weight - I am unable to understand why is it considered light-weight ?

EDIT 1

Other differences: - The design can be viewed in the ASPX engine while it cannot be viewed in Razor engine.

Also, apart from these differences, are there any other differences which make Razor and ASPX significantly different ?

If I am missing any other basic difference/point here, please point it out since I have been able to summarize only the above after reading several articles.

AnkitMittal
  • 166
  • 2
  • 18
  • ASPX View Engine doesn't support TDD (Test Driven Development) Razor Engine supports TDD (Test Driven Development) – Shanker Paudel Apr 22 '14 at 14:07
  • 2
    ASPX View Engine support design mode in visual studio means you can see your page look and feel without running the application. Razor Engine, doesn't support design mode in visual studio means you cann't see your design page look and feel. – Shanker Paudel Apr 22 '14 at 14:07
  • @Shanker Paudel : Agreed that we cannot see the design in Razor. But regarding TDD, I believe that there is an option to create Unit test projects in both ASPX as well as Razor. So, if I understand it correctly then both support TDD. Please advise. – AnkitMittal Apr 22 '14 at 14:35
  • Razor syntax is very lightweight as it doesn't depend on the same pipeline that ASPX does. Because of that, one would consider Razor to be just a parsing engine. One of the advantages of that is that you can have a Razor parser run against any string, where aspx needs an httpcontext and other heavyweight elements. http://stackoverflow.com/a/4702313 – chridam Apr 22 '14 at 14:45
  • 1
    Web Form Engine doesn't support TDD (Test Driven Development) since it depend on System.Web.UI.Page class which makes the testing complex. – Shanker Paudel Apr 22 '14 at 14:50

2 Answers2

2

I think "lightweight" might be a reference to the output from the razor engine: thinking about the output from a traditional aspx forms app the server delivers a page with not only the markup but the viewstate as well, control ids are bulked by effectively a path which describes how they are nested on the page and then there is all the javascript which makes this and things like postback work. In addition updates to the page are more likely to involve a round trip to the server (they needn't but most often they do). On postback the server also has the job of serialising and deserialising not only the viewstate but also instantiating all the controls on the page before you can code against the dom. There are optimisations that you can do but essentially your going to end up with something that looks a bit like this and all of this could be described as heavy - heavy payloads to the client, heavy workload on the server serving the pages and servicing the pages.

In contrast the output from a razor built form can be little more than the markup - simple HTML controls a few hidden fields for key values etc and a button or two. You can add CSS and jquery and dynamic dom manipulation but they aren't essential for making the page: a submit button a form with an action and a method will get your form data back to the server and that's all the server gets a collection of basic encoded form fields. The server will instantiate the expected entity type based on the form fields (it needn't) but that's all it has to do before your coding against the posted data.

In this way the razor model could be considered lighter than the trad aspx way. Even complex pages tend to push the processing to client via javascript/CSS/jquery libraries and ajax communication.

Hargrovm
  • 1,053
  • 8
  • 10
-3

Main advantage of Razor view are :

1) optimized syntax for HTML generation using a code-focused templating approach, with minimal transition between HTML and code

2) Supports layouts (alternative to master page in aspx)

3) Unit testable (Most important in large projects)

4) Supports IntelliSense.

5) The Razor View Engine prevents Cross Site Scripting (XSS) attacks by encoding the script or HTML tags before rendering to the view

6) Razor has a syntax that is very compact and helps us to reduce typing.

Tatipaka
  • 190
  • 2
  • 13