There is a recurring topic when you start developing with AngularJS and Asp.Net: do I use Razor for generating my views or just static HTML?

As an Asp.NET developer myself, I see the tendency in going with the former: after all, that is what you’ve being doing so far and it just works, the Razor syntax is great, plus things like localizing your views are a piece of cake. But there’s more than meets the eye, and that is what’s going to bite you down the road.

Let me underline a few key points:

  • JavaScript frameworks assume you will be maintaining some state in your views and rendering your UI based on JavaScript models. This will definitely interfere, sooner than later, with your server-side rendering.
  • There’s no way of accessing your Razor views directly from the server, so you’ll have to resort to a custom controller just to serve your AngularJS templates as partial views.
  • Once you go down the path of dynamically serving your views, you lose the ability to use a CDN to cache those views (only static content can be served through a CDN). Static HTML is more easily cached by the browser, too.

Asp.NET MVC is a great framework to host and bootstrap your AngularJS app, though. In fact, we saw how to take advantage of the Asp.NET Bundling and Minification capabilities to speed up our AngularJS apps, or how to embed some JavaScript objects directly into the page using Razor for initializing data as opposed to making an extra AJAX call.

So there is plenty of benefits in hosting your AngularJS app with Asp.NET MVC, IIS and all the great tooling that comes with them. Just treat your AngularJS templates like regular HTML, and serve them as static content publicly accessible or bundled together.

I hope this gives you some more insights on the topic.


Templating , Razor , Asp.NET , AngularJS

Written by