Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I come from using ASP.NET MVC/Web API and now I am starting to use Angular but I am not clear on the proper way to mix them.

Once I am using Angular does the MVC sever side concepts still provide any value ? Or should I strictly be using Web API purely to get data for the angular HTTP calls ?

Any tips you have for a ASP.NET MVC guy transitioning to Angular would be helpful

share|improve this question
4  
It would depend, but I would think "should I strictly be using Web API purely to get data for the angular HTTP calls" is correct way to do it. –  MikeSmithDev Jan 13 at 17:42

3 Answers 3

Pure Web API

I used to be pretty hardcore with ASP.NET MVC but since I've met Angular I do not see one reason why I would use any server side content generation framework. Pure Angular/REST(WebApi) gives a richer and smoother result. It's much faster and allows you to build websites that come quite close to desktop applications, without any funky hacks.

Angular does have a little learning curve, but once your team has mastered it, you'll build much better websites in less time. Mainly this has to do with the fact that you don't have all these state(less) issues anymore.

For example imagine a wizard form with any traditional server side framework. Each page needs to be validated and submitted separately. Maybe the content of the page is dependent on values from a previous page. Maybe the user pressed the back button and is re-submitting an previous form. Where do we store the state of the client? All these complications do not exist when using Angular and REST.

So ... come over to the dark side ... we've got cookies.

Similar question

share|improve this answer
16  
+1 for cookies. –  calebboyd Jan 13 at 18:14
    
I agree re: the speed and smoothness of developing apps via Angular. In MVC or WebForms (ugh) you spend this time getting it to spit out markup, and then have to do a couple of passes bolting client code onto it, which feels weird, is labor intensive, and because of the disconnected nature, wastes a lot of time doing simple things. –  HackedByChinese Jan 13 at 18:15
    
Thanks, I also was wondering why we need both and came here. I see in the following post that not using MVC "makes your site invisible to search engines". stackoverflow.com/questions/19609148/… What's the deal with that? –  Narayana Jan 30 at 15:58
1  
@Narayana; This provides some SEO love. –  null Jan 30 at 19:39
2  
@Sleeper Smith: I've had this argument many times with people on my team, but in the end code conventions, unit testing and separation of concerns offers a lot more than all the type safety in the world. I too would love to see JS to be replaced by something like C#, but in reality that's just not the case. All that matters is the result you can achieve. Your customers probably don't care about your coding preferences, they want a cool looking, smooth performing website and they want it quick. Angular gets that job done. –  null Mar 20 at 6:13

AngularJS is more associated with the single page application paradigm, and as such, doesn't benefit much from server-side technologies that render markup. There is no technical reason that precludes you using them together, but in a practical sense, why would you?

An SPA retrieves the assets it needs (JS, CSS, and HTML views) and runs on its own, communicating back to services to send or retrieve data. So, a server-side technology is still necessary for providing those services (as well as other means such as authentication and the likes), but the rendering parts are largely irrelevant and not particularly useful because it's a duplication of efforts, except MVC does it on the server side and Angular does it on the client. If you're using Angular, you want it on the client for best results. You can make Angular post HTML forms and retrieve partial views from MVC actions, but you'd be missing out on the best and easiest features of Angular and making your life harder.

MVC is pretty flexible and you could use it to service calls from an SPA application. However, WebAPI is more finely tuned and a bit easier to use for such services.

I've written a number of AngularJS applications, including a couple that migrated from pre-existing WebForms and MVC applications, and the ASP.NET aspect evolves towards a platform for delivering the AngularJS app as the actual client, and for hosting the application layer the client communicates to via REST (using WebAPI). MVC is a fine framework, but it usually finds itself without a job in these sorts of applications.

The ASP.NET application becomes another layer to the infrastructure, where its responsibilities are limited to:

  • Host the dependency container.
  • Wire the business logic implementations into the container.
  • Set up asset bundles for JS and CSS.
  • Host WebAPI services.
  • Enforce security, perform logging and diagnostics.
  • Interfacing with application caches for performance.

Another great thing about an SPA is it can increase bandwidth of your team. One group can blast out the services while the other lays in the client app. Since you can easily stub or mock REST services, you could have a fully working client app on mock services and swap out for the real ones when they're done.

You do have to invest up front on Angular, but it pays off big. Since you are already familiar with MVC, you have a leg-up on some of the core concepts.

share|improve this answer
    
Ok, so I want to go with Angular and have a .NET back-end to keep my productivity with the API, Entity Framework, security, etc. Do my pages still need a server-side master-child configuration/layout? What do I use for that - WebForms (nope - then I get viewstate), MVC, ASP.NET Web Pages, or something else? –  Sean May 6 at 10:08
1  
Technically, you do not need any server pages, layout pages, etc. Just straight HTML will do. However, depending on the size and complexity of your application, you may want to break your Angular app into several distinct applications by functionality (and you can link from one app to another using a common nav or whatever). In that case, you could use a layout page to keep things DRY and consistent. Furthermore, you're probably using asset bundles for CSS or scripts, and having root pages that are Razor or whatever would bring those things together well. –  HackedByChinese May 6 at 10:37

It depends on the project you are working on.

If angularJS is something new for you I would rather pick an small low risk/pressure project to get started and ensure you learn how to do things in the right way (I have seen in many project making a wrong use of Angularjs becuase of pressure, deadlines... lack of time to learn it in a proper way, e.g. using JQuery or accesing the DOM inside the controllers, etc...).

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

If it's an existing project, you can pickup a complex subset of functionallity and build that page as a separate angularJS app (e.g. your app is composed of a big bunch of standard simple / medium complexity Razor based pages but you need and advanced editor / page, that could be the target piece to build with AngularJS).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.