AngularJS SPA with rendered partial views instead of static html files

时间:2016-02-12 20:37:45

标签: angularjs asp.net-mvc model-view-controller asp.net-web-api single-page-application

I'm making a single page application using AngularJS. For my back-end, I am using ASP.NET 5 (Renamed to ASP.NET Core) with ASP.NET Web API and ASP.NET MVC 6.

I am wondering if it is bad practice to use partial razor html views(basically html files with C# that render into HTML) passed from the MVC controller instead of having static HTML files in the presentation layer.

Basically my options are:

  1. "Classic" AngularJS SPA where all of my html files are static and in the presentation layer, and all presentation logic is done within the browser. The only communication it has with the back-end will be the API calls in JSON.

  2. AngularJS SPA where there are no static html files. Instead, when calls for views are made, calls are made to the backend controller which returns a rendered partial view - basically allowing you to use both razor views and angular.

Is the second method bad practice / is there something bad about it that I'm missing? Thank you.

EDIT: It is easy to find some questions/tutorials on how to implement it this way, but I'm still not sure if it's good practice to do it this way.

1 个答案:

答案 0 :(得分:1)

In my opinion, it's not bad practice indeed to use razor syntax for more dynamic (server side) views. The views are rendered to HTML anyway, so Angular can parse it just like static HTML-files. The advantage of using razor is that is makes it easier to implement server side validation.

Especially when you have security requirements, but in general I suggest to implement server-side validation instead of only relying on client-side validation. It's quite easy to circumvent it and you don't want to end up with vulnerabilities or invalid / inconsistent data leading to server-side errors.