0

If ASP.Net MVC is used for returning data and displaying it in a View, and the Web API is used to transfer data, when and how would somebody display any data coming back from a Web API call?

I'm confused under what circumstances you would use one over the other. I've researched this on the web before posting and still cannot get a simple distinct answer.

I understand the Web PI is used for exposing RESTful services through HTTP, but after the data is retrieved (via a GET), what's the point of using the Web API if you can't display the data?

Any succinct explanation would be appreciated.

2
  • You typically wouldn't have MVC call Web API to get data, as it can just get it directly. What Web API is useful for is to allow JavaScript to make AJAX calls (possibly from an HTML page you rendered with MVC)... Commented Feb 16, 2014 at 21:56
  • Sorry Anthony, still don't get it.... If that's the case, what's the point in using ASP.Net MVC over Web API? Correct me if I'm wrong, but for the Web API url in the AJAX call, you'd have to specific a distinct endpoint via a numbered url because none would exist via the standard MVC Controller/ActionMethod call. Correct? Commented Feb 18, 2014 at 20:42

3 Answers 3

1

You can use client side javascript framework such as

to retrieve and display your returned data.

Take a look at this tutorial for Knockout: http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1

Or this one for Angular: http://code.msdn.microsoft.com/CRUD-Grid-Using-AngularJS-84afaf13

Sign up to request clarification or add additional context in comments.

1 Comment

Lars, thanks for the links. I'll take a look at the Angualar one. However, in my questions to Brandon, for now, I'm looking for a simple way to do this with ASP.Net MVC, Ajax and jQuery.
0

Web API is a framework for building web services that leverage all aspects of HTTP - ASP.NET MVC is a framework for building websites.

You could use the two together; for example, you could use ASP.NET MVC to generate HTML "templates" server side and then use JavaScript on the client side to invoke your Web API resources to get and modify the data and "plug it in" to your HTML templates. Check out @Lars' answer for more info on that particular scenario and the JavaScript frameworks that make that easier. This technique is mostly used for creating more fluid user experiences over traditional websites.

A big reason to choose Web API instead of putting everything in an ASP.NET MVC website is if you ever expanded beyond a single website to multiple types of clients (e.g., smartphones, Mac / Windows applications, etc.). Since ASP.NET MVC is really geared for websites / generating HTML, whereas native clients tend to prefer simple web requests and rendering the UI client-side, a simpler approach would be to do all of your data manipulation behind a Web API and have all of your UIs consume it.

There's a ton more to say about Web API and when you'd choose it over (or in concert with) ASP.NET MVC. I actually think the more interesting tradeoffs come when you compare it to SOAP-based service stacks like WCF.

5 Comments

Brandon, how would a typical call from an AJAX request look like to a Web API method via jQuery? Since the Web API is essentially a service, somewhere in the AJAX call, you'd have to specify the specific endpoint/url to access along with the Controller/Action method.
So Brandon, are you saying a typical scenario might be to use an ASP.Net MVC application for View generation of data I would retrieve via an AJAX call to a Web API service?
btw, I'm assuming that Ajax call to the Web API method would come through to the appropriate ASP.Net MVC Controller/Action method. I'm just confused how this scenario would look in a simple method call... Under previous circumstance (prior to Web API), the database call would come from Ajax to the ASP.Net MVC Controller/Action method. If you could straighten me out on the use of the Web API with what I said to Lars, that would be great. I guess, that the Web API might be better used with SPA applications like Lars had mentioned instead of with ASP.Net MVC.
@sagesky36 From the client side, you'd have no idea if the server-side was using MVC or web API (e.g., you could do a $.get("api/questions", ..., and in web API you'd likely have a QuestionsController that inherited from ApiController with a Get action to return you the data - but all of that is configurable and the setup I described is just the convention.
@sagesky36 as I said, it's great for SPA applications, but it's also great for mobile applications, and I also prefer it for synchronous service calls in a service-oriented architecture (although WCF and MSMQ is still preferred for one-way, fire-and-forget operations)
0

You can use Telerik kendo UI Web which is free under GPLv3. It uses knockoutjs.

Download Kendo UI Web Open Source

Kendo UI Demos

There are a lot of free ones out there, but I just happen to use that kendo UI.

1 Comment

Thanks, but I'd rather stick to pure JavaScript, Ajax, and jQuery with ASP.Net MVC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.