ASP.NET MVC Razor view vs AngularJS
Asked Answered
Q

2

7

I am using ASP.Net MVC with Razor syntax in Views. But with little research on performance enhancements I have found that View Engines takes a time to compile razor code to HTML. So is it good idea to use AngularJS in Views over razor syntax? If this improves performance then what are the other pros and cons of it?

Thanks in advance.

Quarry answered 26/10, 2017 at 15:9 Comment(1)
Possible duplicate of this questionGamely
M
6

Which route will give you better performance is dependent on several factors. zambeb's answer regarding mvc.net razor views is not accurate.

mvc.net views are compiled the first time they are ever requested and thereafter only data is plugged into the pre-compiled view. This is a much less processor intensive operation than angular views being parsed and rendered.

However, angular views are rendered on the client (browser). If for some reason your server is insufficient for your needs your site will perform better if built with angular.

On the other hand if both sites are built with adequately sized servers an mvc.net will tend to have faster render times, because the browser has a less intensive job.

The exception to this is of course if you are building a site that will have many small ui changes that you wish to make with views instead of through dom manipulation or widgets, then again angular wins.

This is why when building with mvc.net it makes sense to build slightly larger and more complex views than angular, not that you must.

In the end each approach can be abused and under perform. Each approach also lends itself to a particular style of site.

These two approaches can also be combined. In mvc.net a layout page can be made to render views as html fragments. This means a front end angular developer only need worry about static views and controllers and the mvc.net developer concerns himself with models, dynamic views, and controllers on the server.

Good luck!

Millisecond answered 10/1, 2018 at 0:27 Comment(2)
I think you should look into how precompiled views work. They're highly optimized. While your updated answer is better, the statement that each request creates a new html file is wrong. It misrepresents what is happening and implies an amount of work that is much greater than is occurring.Millisecond
I have been working with Razor since its first version. And is still good. The client receives an html anyway. Please clarify how the client obtains this html if it is not generated "somewhere". And also clarify where is it generated and which part of my answer says the otherwise and where is it promotional. My initial post said exactly the same, the edit was only to add the point for the "precompiled" mention, which nobody is discussing. Don't confuse people pleaseKano
K
0

Razor is a server-side view engine. This means that every request you do, will be took on the server side and will produce a new html page (from a precompiled view, as @N-ate have added, but still from the server side). It is like to creating or reading html file on the server and then send to the client on the response. In other the side, Angular doesn't need to generate new html files. It works directly with data sent from the server to generate the view directly on the client side (browser).

Both approaches are fine, depending on the situation. Will you prefer your server working hard to read or create html files, or it is better to you to let the clients to do this work?

Another think to consider is your UI designers work-flow. With Razor you need designers to know some of the Razor syntax, which is very rare this (and those) days. With Angular you can expect to find a lot more of people used to this workflow.

There are more differences, but I think the decision can be narrowed with these two points in mind.

Kano answered 26/10, 2017 at 18:9 Comment(2)
zameb your post is promotional and you have misconceptions about what work is performed by the server in mvc.net. You seem to think it is more than what is performed by the client in angular, which it is not. It is actually less, because no string parsing is performed in mvc.net as it is in angular. Though you shouldn't compare these 2 because of where the work occurs.Millisecond
I love both technologies. In fact, I was more happy when no Angular-wise libs/frameworks ever existed. It is not my intention to promotion anything. But nothing is free in the universe. When 10000 clients hit the server-side Razor Engine, the server has less work than 10000 clients, but the work is distributed on all clients. Well, I may be still wrong. Please feel free to move this to a chatKano

© 2022 - 2024 — McMap. All rights reserved.