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 am new to the Asp.Net MVC. I am doing asp.net web application. I have data layer which I want to be exposed from the MVC Web api only. For web api project i used entity framework as a model and for mvc application i am using entity classes. I am calling the web api from the view with jquery and perform all the operations.

Is that right approach or need to use some jquery framework. Or is there any projects available which uses asp.net web api's only. I warm up google for so many times but didn't find essential. Please assist me here.

share|improve this question
    
Do you need access to the data layer on the service side(MVC Controller)? –  sunil May 14 '14 at 19:44
    
@sunil : I want to access it from web apis –  eraj May 15 '14 at 4:54
    
Learn the basics of MVC websites first, as it comes with many unobtrusive (Ajax + jQuery) features that you just need to enable. Then apply a layer of client-side jQuery as needed to enhance the user experience. (side-note: Typically I maintain EF models in a separate library, with all extension methods and business logic, and include that into my web projects). –  TrueBlueAussie May 29 '14 at 9:53

2 Answers 2

up vote 1 down vote accepted

You can use jQuery or any javascript framework you please to retrieve data from your API. You could even use raw javascript if you really wanted to. However, if most of your logic is going to live on the client, you'll probably want to look into some MVC/MVVM/MVP javascript library/framework to keep things organized.

A good place to start is the Single Page Application template that is built into Visual Studio 2013 and available for 2012. It uses .NET MVC to load up the HTML markup, styles, and scripts, and then uses Knockout.js to structure the application and send & retrieve data from a Web API controller located in the same app. It's also worth noting that you can grab templates that use other popular javascript frameworks like Ember, Angular, Backbone, Durandal, and more. If you'd prefer one of those frameworks over Knockout then download one of those templates and look through the code.

share|improve this answer

Without seeing any code, it sounds like you have the right idea. You can use any client side framework of your choice to interact with your Api Controllers (Knockout, Angular, Ember, Backbone) or you can use straight up jQuery.

Since you are new to ASP.NET Mvc, a little clarification on the differences between the traditional Mvc Controller and the new Api Controller seems in order. In traditional MVC the controllers works to select which view (UI) to display married to data (the model):

public ActionResult Index()
{
     var theData = serviceLayer.GetData();
     return View("Index", theData);
}

You can also return pure JSON data from a controller:

public JsonResult GetMyData()
{
     var theData = serviceLayer.GetData();
     return new JsonResult()
     {
          Data = theData
     };
}

This had the problem of making it difficult to determine the names of the endpoints that retrieve UI vs the endpoints that retrieve data. The Web Api is Microsoft's implementation of a RESTful framework. Instead of a controller with many different methods (Index(), GetSingle(), GetMany(), List(), DoSomethingElse(), DoAnotherThing(), etc.), the Api controllers usually contain only methods that correspond to the HTTP Verbs: GET, POST, PUT, and DELETE. The same endpoint can then be called differing only by the verb you are using. Take the following endpoint for example:

http://mydomain.com/api/customer/1234

Calling this endpoint will do the following for Customer with id=1234:

  • GET will return the data
  • PUT will update the data
  • DELETE will delete the data

In addition to making it clearer for your own development team, the Api also makes it easier for you to expose your data to third parties. You would need to provide a root URL and a list of your objects and any developer would be able to use it. There are other things you would need to do such as implementing security tokens but I wanted to demonstrate the benefits of using an easily understood framework.

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.