views:

407

answers:

4

A Guide to Designing and Building RESTful Web Services with WCF 3.5, this article explains the foundations of REST and how it relates to WCF. MVC uses REST as the architectural model. I am guessing one can use the .NET MVC to create web applications that have both a front end and an API point, but I am not sure if the safe way of building the API is to build it with WCF and then use it in the MVC as a controller.

Please comment if the question is not clear, I will add or modify the text.

+1  A: 

ASP.NET MVC can serve as a REST endpoint for light services work, so I guess the answer to your question depends on how you define "safe."

Clearly WCF is designed specifically for creating REST endpoints, with all of the security implications that are implied thereof, whereas ASP.NET MVC is designed to create REST endpoints which can be used by ASP.NET MVC itself.

The following article shows how to create a web service using an ASP.NET MVC controller:

Create REST API using ASP.NET MVC that speaks both Json and plain Xml http://msmvps.com/blogs/omar/archive/2008/10/03/create-rest-api-using-asp-net-mvc-that-speaks-both-json-and-plain-xml.aspx


See also the following article from Phil Haack, which discusses an SDK the WCF team put together for users of ASP.NET MVC:

Rest For ASP.NET MVC SDK and Sample
http://haacked.com/archive/2009/08/17/rest-for-mvc.aspx

Robert Harvey
+2  A: 

Theres actually a third option, ADO.NET Data Servies. Anyway, here how I see them.

MVC REST: Gives you full control over how to expose your data, you have to write all the code to get it up an running tho, e.g. serialization, deserialization, all the CRUD methods etc etc. Worht metioning that this being an MVC site means you are limited to exposing your service via IIS over HTTP(S)

WCF REST: More automation than MVC, a much more solid frameowkr than MVC REST, i.e. caching, security, error handling etc (basically all the stff you'd have to write yourself using plain MVC). Being WCF, you can host this in a variety of ways (e.g WS-, TCP) etc.

ADO.NET DATA SERVICES: The quickest way to get up an running with everthing ready to use, all you need todo is configure the global.asax, however you have to use an Entity Data Model, which you many not want to.

Personally, I would use either ADO.NET DATA SERVICES or WCF REST to build an API, consue that API in MVC site and then expose that API either directly, or by passing it through another layer.

Jaimal Chohan
Actually, you don't have to use Entities to use ADS. ADS can work with Linq to SQL, or even a non-relational data source (with a bit more work).
Matt Brunell
A: 

They are two different sets of technologies, only related by being built on .net

MVC is used to create websites and provides a model where URLs are routed to controllers and controllers deliver views to the user as the user interface.

WCF is a set of libraries in .net that are used to abstract the type of service (is it hosted in a windows service, as a webservice in IIS etc.) as well as the protocol (HTTP, TCP, MSMQ etc.) from the client and server which are communicating.

An MVC website may use WCF to connect to a web service, but that is just one of many options.

TJB
A: 

ASP.NET is used to build websites.

MVC is a development pattern used to make your ASP.NET lossely coupled. Meaning it is a separation of concerns that aims maintainability, extensibility and testability of the application.

Model is the classes used to represent data from SQL Server or any other services.

View is the ASP.Net Pages itself

Controller is the logic that manages the Model and View, which view to display. Uses the model as objects.

WCF is a part of .NET framework 3.0 that aims to combine different service technologies such as Web Services, SOAP and some other service architecture into one API.

ASP.NET = Website

MVC = Design Pattern (Try to See MVP and MVVM for comparison)

WCF = Services

Therefore going back to the question, how does ASP.NET MVC relate to WCF. ASP.NET MVC can consume WCF Services. If you have a service that sends you data, it should be in the Model where you represent the data as classes. If your WCF service is for operations, it should be in the controller.

References: http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

Lawrence A. Contreras