A common question that comes up is when to use ASP.NET MVC to build out REST-ful services and when to use WCF?
when the only reason for the service’s existence is to service the one application you’re currently building, it may make more sense would stick with the simple case of using ASP.NET MVC. This is commonly the case when the only client to your JSON service is your web application’s Ajax code.
When your service is intended to serve multiple clients (not just your one application) or hit large scale usage, then moving to a real services layer such as WCF may be more appropriate.
There are several reasons to consider using XHTML as the default representation for your ASP.NET MVC RESTful services. First, you can leverage the syntax and semantics for important elements like <a>, <form>, and <input> instead of inventing your own. Second, you’ll end up with services that feel a lot like sites because they’ll be browsable by both users and applications. The XHTML is still interpreted by a human—it’s just a programmer during development instead of a user at runtime. This simplifies things throughout the development process and makes it easier for consumers to learn how your service works. And finally, you can leverage standard Web development frameworks to build your RESTful services.
ASP.NET MVC is one such framework that provides an inherently RESTful model for building XHTML-based services. This article walks through some XHTML design concepts and then shows you how to build a complete XHTML-based RESTful service.
XHTML: Representing Data and Links
XHTML: Representing Input with Forms
Understanding the ASP.NET MVC Architecture
Implementing the Model
Implementing the Controller
Designing URIs with Routes
Implementing the Views
Consuming the Bookmark Service
What is the Asp.Net MVC Membership Starter Kit?
The starter kit currently consists of two things:
- A sample website containing the controllers, models, and views needed to administer users & roles.
- A library that provides testable interfaces for administering users & roles and concrete implementations of those interfaces that wrap the built-in Asp.Net Membership & Roles providers.
The ASP.NET team just released the first public preview of ASP.NET MVC Version 2. You can download it here.
DataAnnotation Validation Support
Strongly Typed UI Helpers
UI Helper Templating Support
- New [HttpPost] Attribute
- Default Parameter Values
- Binding Binary Data
The first part is easy, just grab the Unity DLLs and reference them in your MVC project. At this point, you’re now free to use Unity to inject whatever you feel like. Rigging up the controller factory so that it will use Unity to resolve controllers is equally easy because the mvc contrib codeplex site has already created a class called UnityControllerFactory that does just this. Once you’ve added references to Unity and Mvc.Contrib.Unity from your MVC project, you can change your global.asax.cs file to look something like this:if (_container == null)
_container = new UnityContainer();
_container.RegisterInstance(typeof(AccountController), new AccountController());
The installer is now available on CodePlex. Be sure to give it a try as many of the new VS10 features intended to support the TDD workflow fit very nicely with ASP.NET MVC. You can also check out how to create a new Controller the TDD way here.
Update: a new version support MVC 2.0
To install the templates, just extract the zip file from the link below and run installNUnit.cmd. If you are installing on Vista or higher, run the cmd file as an admin.
Download -> NUnit Test Templates
A 185 page end-to-end tutorial that walks-through building a small, but complete, ASP.NET MVC application from scratch.
- Download the free end-to-end tutorial chapter in PDF form
- Download the source code + unit tests for the completed application
- Learn more about the book from the official Wrox page
- Purchase the full book from Amazon.