4

I've some confusions to transfer data between angularjs and MVC5. I am creating single page application (SPA).

  1. What is the best way to work with angularjs? Is MVC controllers or MVC APIs ?.
  2. I read it here that api's are good for SPA. So, do I need to build a different project for APIs ?
  3. What about asynchronous communication for Angularjs with MVC ?

I am not getting a right direction to work ahead. I am familiar with mvc but not APIs. So, Please guid me a right way !!

Thanks in advance

Sarbanjeet
  • 253
  • 1
  • 15
  • @AntP thanks for google suggestion but my question is about confusions. As i already created models and classes in mvc. I want to understand perfect scenario for communication. – Sarbanjeet Jan 09 '15 at 08:19
  • That is a very broad question, the answer to which will vary massively dependent on context and will be largely subjective anyway. Your best bet is to learn about both technologies, read some blog posts comparing the two and make your own mind up that way. – Ant P Jan 09 '15 at 08:21
  • Ok, If you have any information about right direction please let me know. Thanks – Sarbanjeet Jan 09 '15 at 08:26
  • 1
    http://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc Personally, I would go with API. Having API methods returning json data and having angular to take of views is a great combination. – Ivan Sivak Jan 09 '15 at 08:27
  • 1
    @ivan.sivak thanks, I am confused with this new approach vNext http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6 – Sarbanjeet Jan 09 '15 at 08:36

3 Answers3

4

First of all, with the current version of Visual Studio (2013) there's no distinction between a "web form" project and an "mvc" project. There's just a web application project and inside you can put whatever you want.

Now from my experience a nice and clean way to approach your problem is to create normal MVC controllers to render the razor views that contain the angularJS application(s), and to create WebAPI controllers for the RESTful interface for the ajax methods.

In angularJS you don't really need to manually do your ajax calls. There is a more convenient and powerful way: resources. They also play well with the WebAPI design, since a WebAPI controller works with a single type of object (i.e. a customer) and through HTTP VERBS allows you to do the CRUD. For instance:

// assume we have a backend repository that handles the data

public HttpResponseMessage Get()
{
    return this.Request.CreateResponse(HttpStatusCode.OK, this.repository.GetAllCustomers()); 
}

public HttpResponseMessage Post(Customer customer)
{
    var modifiedCustomer = this.repository.Update(customer);
    this.repository.SaveChanges();

    return this.Request.CreateResponse(HttpStatusCode.OK, modifiedCustomer);
}

This method queries all the available customers and returns them. You don't define here whether to return JSON or XML: the WebAPI framework reads the HTTP HEADERS of the WebAPI request and serializes the data as requested from the client. For JSON, which you'll be likely be using, it does the serialization through the default JSON serializer defined. You can override this to change the way JSON is created, a common way is to use JSON.NET with custom settings.

AngularJS resources are made to map a single URL and work with all the verbs internally and expose you methods like $save, $query, $get and so forth, so they couple up very well. For instance:

var customerRes =  $resource('/customers');
var currentCustomers = customerRes.query(function(){ // query is mapped to the GET verb
    currentCustomers[0].email = "foo@baz.bar";
    currentCustomers[0].$save(); // default mapped to the POST verb
});

I suggest you to check the documentation and samples for more details.

Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
2

You can use MVC controller functions with Async and MVC API both. In Controller Async methods you need to handle all responses manually as API have inbuild many features. In controller Async methods your application will handle self calls. Of course API gives you more flexibility to work on different application types.

More thing you need to worry about when your MVC application have APIs for different type of application likewise mobile apps etc. 1. Pool Services. 2. Threads per worker process in IIS. These will define scale of your application when you have APIs or Async methods in application for different type of application.

Vicxx
  • 444
  • 4
  • 10
1

1) You can create Simple Controller - and inside create methods that will return JsonResult And call thats methods from Angular via AJAX

2) Yes - If you want build API - you need create new project type of WebAPi (right now is v 2.0) - but you can create it in one solution with SPA

3) You can call ajax asynchronous - its not a problem

David Abaev
  • 690
  • 5
  • 22
  • appreciated your answer, But Angularjs is good with asynchronous inbuilt methods, So to work with it, API required ? Do api's will effect my project cost, work cost, machine cost? – Sarbanjeet Jan 09 '15 at 08:42
  • First of all API - its good solution if your project will have not only WebSite, but more platforms ( like mobile applications etc) If yes - you must build API - for All platforms will get data from one place. if you will have only website - you dont will feel very different if you will have API or just controllers with methods – David Abaev Jan 09 '15 at 08:46
  • MVC 6 coming with new feature [vNext](http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6). Its help to force a controller to behave as API. Do it useful? – Sarbanjeet Jan 09 '15 at 08:57