Solved

How can ASP.NET MVC4 use Web Services

Posted on 2013-01-10
8
439 Views
Last Modified: 2013-01-31
Hi, I am fairly new to asp mvc, I have been through some walk-thoughs and understand the overall principal, what I am struggling to grasp is where web services fit in.

So lets say I want to build a web app that will run on browsers and mobiles (via a browser), this is all doable with straight mvc and some clever views that will serve pages properly for the target device - all fine.
Now if somewhere down the line I want to write dedicated mobile apps, my understanding is that these apps need to communicate with web services but now my whole backend is mvc controllers - so by my thinking I should really be creating an mvc patterned app that uses web services to drive its data and logic.

1. Am I reasoning this out correctly or am I mixing technologies that are unrelated?
2. What is the best way to approach an a dev project that will target a wide range of devices?
0
Comment
Question by:Alw1n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38763161
My understanding is that mobile devices benefit from RESTful services. This is because RESTful services operate much the same way web pages do, and since you can already pull up  a web page in mobile, a RESTful service can be done in (typically) the same way.

You might consider looking into the Web API that is new in .NET 4.5 (and installed separately in 4.0). You can create a RESTful architecture that is patterned on the MVC architecture. You have models and controllers, but no views. There is built-in content negotiation that allows your clients to specify what format they would like the result returned in (e.g. JSON, XML) by simply including a Accept header in the request with the appropriate identifier.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38763167
P.S.

In case it wasn't clear, RESTful services are easily consumed in a desktop environment as well.
0
 

Author Comment

by:Alw1n
ID: 38766098
Your comment 'You have models and controllers, but no views' is really puzzling me. I have briefly looked into Web APi and IIUC it is just another way of creating a web service that provides xml/json data for any endpoint that may need it ie. I see it more closely related to the 'controller' in mvc, not the 'view'.

The two options I see are:
1. Have bunch of WebAPI's as a backend which can provide data to mobile apps and to web clients directly
2. Have an mvc architecture that is web only

but I still can't see how web api can be used in conjunction with mvc to serve mobile and web apps without duplicating controller logic in the web service
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38767139
it is just another way of creating a web service that provides xml/json data for any endpoint that may need it
By default, yes, but you can write the service to return much more than just XML or Json. And with automatic content-negotiation, you can write an extension that the runtime will call, and you can let your clients dictate the kind of response they prefer, and you only have one piece of logic within the service method itself (only the extension is added/modified).

I see it more closely related to the 'controller' in mvc, not the 'view'.
Which is why I said that there are no views  ; )

Web API is styled after MVC, but it is not truly MVC--there is no "V" in Web API. Yes, you have controllers the same way you have them in MVC, and they function the same. You may or may not have models (more than likely, you will).

I still can't see how web api can be used in conjunction with mvc to serve mobile and web apps without duplicating controller logic in the web service
Well what kind of data are you serving? MVC is going to be used to give a visual representation to some data for the benefit of the user. Web API (or any web services for that matter) is going to be used to retrieve some data for an end-user application. There is not visual representation for this data--it's up to the caller to provide this representation. Typically, web service calls are invoked via AJAX when dealing with web pages.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38767525
I'll put together a sample project later this evening (EST).
0
 

Author Comment

by:Alw1n
ID: 38773563
I sort-of get what you are saying, a sample project will be great to visualize, much appreciated.

As a side-line, I have seen very little on the web regarding this, is it too new or is it an uncommon problem, or am I just approaching this the wrong way?
0
 

Author Comment

by:Alw1n
ID: 38774332
I loaded up my first Web API project to see what it's all about and was surprised to find a 'Views' folder and a regular 'mvc' contoller along with the ApiController.

To me this implies that a new Web Api project already has all the bits to provide an MVC web app + any web services associated with that app.
Now if I design my app with the API controller talking to EF and returning json, any mobile app could use those services  + if my mvc controllers get their data from the api controllers I am home dry with an MVC styled web app that is ready for mobile apps to plug into.

Comments?
0
 

Author Comment

by:Alw1n
ID: 38835397
I didn't get the sample app from kaufmed but it looks like what I am saying above is viable so I will close thid quesiotn
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question