Mixing Angular and ASP.NET MVC / Web api?

I am using ASP.NET MVC / Web API and now I am starting to use Angular, but I do not know how to mix them correctly.

Once I use Angular, do MVC concepts still provide any value? Or should I strictly use the Web API only to receive data for Angular HTTP calls?

Any tips you have for ASP.NET MVC, migrating to Angular will be helpful

+80
angularjs asp.net-mvc asp.net-mvc-4
Jan 13 '14 at 17:28
source share
3 answers

Clean web interface

I used to be pretty hardcore with ASP.NET MVC, but since I met Angular, I donโ€™t see one reason why I will use any server-side content generation structure. Pure Angular / REST (WebApi) gives a richer and smoother result. This is much faster and allows you to create sites that are close to desktop applications, without any funky hacks.

Angular has a small learning curve, but once your team has done it, you will create much better sites in less time. This is mainly due to the fact that you do not have all of these conditions (less).

For example, imagine a wizard form with any traditional server-side structure. Each page must be checked and sent separately. Perhaps the content of the page depends on the values โ€‹โ€‹from the previous page. The user may have clicked the back button and resubmitted the previous form. Where do we keep customer status? All these complications do not exist when using Angular and REST.

So ... come to the dark side ... we have cookies.

Similar question

+105
Jan 13 '14 at 18:05
source share

AngularJS is more connected with the paradigm of a one-page application and, as such, does not really benefit from server technologies that do markup. There is no technical reason that prevents you from using them together, but in a practical sense, why are you?

SPA extracts the resources it needs (JS, CSS, and HTML) and launches on its own, exchanging data with services to send or receive data. Thus, server technology is still necessary to provide these services (as well as other means, such as authentication, etc.), but the parts of the rendering are largely irrelevant and not particularly useful, since duplication of efforts, with the exception of MVC, is on server side and Angular does this on the client. If you are using Angular, you want to get it on the client for best results. You can make Angular submit HTML forms and get partial views from MVC actions, but you cannot use the best and easiest Angular functions and make your life more difficult.

MVC is quite flexible, and you can use it to service calls from a SPA application. However, the WebAPI is more precisely configured and a little easier to use for such services.

I wrote several AngularJS applications, including a couple that was ported from pre-existing WebForms and MVC applications, and the ASP.NET aspect is developing towards a platform for delivering an AngularJS application as an actual client and for hosting the level of the application that the client communicates with through REST (using WebAPI). MVC is a great structure, but it usually fails to work in such applications.

An ASP.NET application becomes another layer of infrastructure where its responsibilities are limited:

  • Installs the dependency container.
  • Plug the implementation of business logic into a container.
  • Customize asset packages for JS and CSS.
  • WebAPI host services.
  • Secure, log and diagnose.
  • Interaction with application caches for performance.

Another great thing about SPA is increasing the bandwidth of your team. One group can disable services, and the other in the client application. Since you can easily drown out or mock REST services, you can have a fully working client application for mock services and change it to real ones when they are executed.

You need to invest forward on Angular, but it pays off. Since you are already familiar with MVC, you have an advantage over some of the basic concepts.

+41
Jan 13 '14 at 18:00
source share

It depends on the project you are working on.

If angularJS is something new for you, I would rather choose a small project with a low level of risk / pressure to get started and make sure that you learned how to do the right thing (I saw a lot of projects that use Angularjs incorrectly due to pressure, timing .. lack of time to learn it properly, for example, using jQuery or accessing the DOM inside controllers, etc.).

If the project is a green field, and you have some experience with AngularJS, it makes sense to abandon ASP.net MVC and go to the server side for pure REST / WebAPI.

If this is an existing project, you can pick up a comprehensive set of functionalities and create this page as a separate angularJS application (for example, your application consists of a large group of standard Razor simple / middle layers, but you also need an advanced editor / page, which can be the target part to build using AngularJS).

+6
May 25 '14 at 10:05
source share



All Articles