ASP.NET MVC Preview 3 includes the MVC Controller changes we
first discussed and previewed with the April MVC source release, along with some additional tweaks
You can continue to write controller action methods that
return void and encapsulate all of their logic within the action method.
which would render the below HTML when run:
Preview 3 also now supports using an approach
where you return an "ActionResult" object that indicates the result of
the action method, and enables deferred execution of it. This allows much
easier unit testing of actions (without requiring the need to mock
anything). It also enables much cleaner composition and overall execution
For example, we could use LINQ to SQL within
our Browse action method to retrieve a sequence of Product objects from our
database and indicate that we want to render a View of them. The code
below will cause three pieces of "ViewData" to be passed to the view -
"Title" and "CategoryName" string values, and a strongly
typed sequence of products (passed as the ViewData.Model object):
One advantage of using the above ActionResult approach is
that it makes unit testing Controller actions really easy (no mocking required).
Below is a unit test that verifies the behavior of our Browse action method
We can then author a "Browse" ViewPage within the
\Views\Products sub-directory to render a response using the ViewData populated
by our Browse action:
When we hit the /Products/Browse/Beverages URL we'll then
get an HTML response like below (with the three usages of ViewData circled in
Note that in addition to support a "ViewResult"
response (for indicating that a View should be rendered), ASP.NET MVC Preview 3
also adds support for returning "JsonResult" (for AJAX JSON
serialization scenarios), "ContentResult" (for streaming content
without a View), as well as HttpRedirect and RedirectToAction/Route
The overall ActionResult approach is extensible (allowing
you to create your own result types), and overtime you'll see us add several
more built-in result types.