Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net combine C# and Angular [closed]

I don't really know how to combine Angular and C#.

Like I have controllers provided with the MVC and Asp.Net, but I have also controllers provided by angular, so what should I use? The routing provided by angular-route or the routing of the MVC? Also should in Angular I have the ng-repeat attribute and in C# I have the foreach.

I don't really know how to combine these two languages in Asp.Net? Could someone explain me the best approach.. Thanks a lot!

like image 345
Tom el Safadi Avatar asked Dec 28 '15 02:12

Tom el Safadi


2 Answers

I can think of two possible ways to implement

  1. Separate AngularJS & C#application completely.
    • AngularJS application will be stand-alone app, handle all UI logics, routing logic, UI template, have own view models.
    • C# application (I recommend you to use WebAPI/WCF or anything you're comfortable with) will expose HTTP endpoints for AngularJS application to call
    • PROS: reusability (the endpoints can be used anywhere, for ex: native mobile app), light-weight (deployment package might only contains a few js files & html files), flexibility (you have the ability to switch the technology easily, for ex: switch the server side to use NodeJS instead), server & client code could be implemented by two different teams, etc...
    • CONS: take much more time to develop, require strong knowledge of AngularJS, sometimes not easy to debug for beginner, need extra effort to secure the endpoints
  2. Mixed-mode
    • You could combine any C# web technology (MVC, WebAPI, Webform) with AngularJS, so AngularJS code become your view or a part of your view
    • With this method, most of your application logics are still handled in MVC layer, you might reuse model classes (need to return as JsonResult for them to be friendly with JS app)
    • PROS: The application is more on C# code => easier for C# devs, easier to test (unit test, automation test your code), easier to implement authentication & authorization logic (MVC do it for you), faster to develop (in my opinion), etc
    • CONS: see PROS part of 1 :).

Hope it helps.

like image 57
Kien Chu Avatar answered Nov 04 '22 11:11

Kien Chu


There are 2 different things. Your C# controllers will run on the server while Angular controllers will run on the browser. When you combine C# + Angular is like building to separate applications: a C# (asp.net) app that runs server-side and a JavaScript (angular) app that runs client-side.

C# controllers will handle http requests while angular controllers will coordinate the interaction of the different Angular components that are running on the browser.

I recommend you this hands-on to get started: https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-spa-with-aspnet-web-api-and-angularjs

like image 7
NicoPaez Avatar answered Nov 04 '22 13:11

NicoPaez