Contributors:

Chris Massey

Controllers Overview

MVC Controller Overview [ASP.NET C# Tutorial]

Published in 2008. Stephen Walther introduces ASP.NET MVC Controllers, and walks through how to create them, and then return different types of action results. This is a very basic introduction.

The Life and Times of an MVC Controller [MSDN Magazine]

Published in 2009. Scott Allen gives a reasonably in-depth walkthrough of Controllers, their role in MVC, and the relevant attributes and types that come with them.

Controllers and Routers in MVC 3 [Code Project]

Published in 2011. Ambily Raj gives a simple introduction to Routing and Controllers.

Creating Controllers

Creating a Controller [ASP.NET]

Published in 2009. Stephen Walther continues his MVC tutorials, walking through the process of creating a new controller in C#.

Are Controllers created for every request? [StackOverflow]

Published in 2011. This question deals with whether a controller is created by the ControllerFactory for every HTTP request, rather than creating controllers in your solution.

Receiving Input

Best Practice for getting user input in your controller? [StackOverflow]

Published in 2011. A conceptual look at how to get user input while doing something in an action method in your controller.

Passing data from a view to a controller in MVC3 [StackOverflow]

Publishing in 2012. A problem involving data being inconsistently being passed back to the controller due to incorrect form structuring.

Parameters

Passing parameters to the controller [StackOverflow]

Published in 2008. An error in routing set up sheds some light on how parameters work in the context of controllers.

Intro to Action Results & Command Patterns

Paul Stovell – Cleaning up Controllers [Blog]

Published in 2012. Paul illustrated how he’s used Command Patterns to clean up his controller code. The comment thread is full of good discussion, and worth a look.

The Command Pattern and MVC Architecture [Code Project]

Published in 2005. David Veeneman gives a conceptual overview of the command pattern, complete with some of its pros and cons.

Rachel Appel – ActionResults Explained [Blog]

Published in 2011. A concise introduction to action results with good visualizations.

Actions in MVC 3 [Code Project]

Published in 2011. Ambily Raj gives some broad coverage of Actions in MVC3, starting with Action results. Plenty of code and screenshots to illustrate her points.

Routes, Actions and ActionResults [HowMVCWorks]

(Probably) Published in 2009. Eric Sowell’s introduction to how routes and actions fit together. This site features plenty of code, and links to good resources. Aimed as an intro-level tutorial.

Unit Testing Controllers and Actions

This is how MVC controller actions should be unit tested [CodeBetter]

Published in 2008. Jeffery Palermo blogs about his experiences in upgrade his website to MVC (using Preview2), and compares the unit tests he wants to write against the unit tests he has to write.

Unit testing simple MVC controllers [Los Techies]

Published in 2010. Chris Missal documents a useful pattern he’s noticed for unit testing ASP.NET MVC, and demonstrates it using RhinoMocks and NUnit.

Building testable MVC applications [MSDN Magazine]

Published in 2009. Justin Etheredge covers the testing support built into MVC, as well as the necessary tooling, dependency injection, and how testability applies at every level of application design.

Rendering a View to Return HTML

How can I return the HTML from an MVC Controller to a &lt:div> in my View? [StackOverflow]

Published in 2012. Troubleshooting a form submission process to return an HTML string to a View. This is fairly direct Q&A, with very little confusion.

Return HTML string from a Controller and display it in a View [StackOverflow]

Published in 2012. A very simple question on how to pass an HTML string from the controller and render it in the View. As a result, the solutions are also very simple / basic. This is not a resouce with a lot of depth.

Passing Data from an Action Method to a View

MVC Quick Tips – How do I pass data to a view? [MSDN Blog]

Published in 2010. Nuno Silva documents two simple ways of passing data to the view: the ViewData Dictionary, and passing it as a paramter on the controller’s action method. A great explanation with plenty of code details.

Redirections

301 Redirect as an Action Result [ASP.NET]

Published in 2008 (with a minor update in 2012). Simply the code needed to handle a 301 redirect from a controller, as the OP cannot set up routes in Global.asax. Essentially zero explanation, just a solution.

Mike Bosch – MVC Tip – Redirecting to another action and passing information to it [Blog]

Published in 2008. A very quick how-to guide for redirecting a user after some controller event. The comment thread is actually longer than the post, and is rich with variations.

How to redirect to action of another controller of a referenced project [StackOverflow]

Published in 2012. An attempt to redirect between projects, resolved using an absolute URL and the Redirect method. This is a very simple and direct Q&A, with zero variations around the solution.

Returning non-HTML Data

How to serve the same data in JSON, XML or HTML [Blog]

Published in 2009. Mark Jensen documents (and supplies the code to control) how to add attributes to your controller or action to handle how data will be returned, and how to control what formats are permitted.

Controller actions that return JSON or partial HTML [StackOverflow]

Published in 2009. How to use a parameter to specify what format your controller actions return data in, and return the data asynchronously. Six answers (of varying depth and complexity), with links to further reading (relevant to MVC3)

Creating a Custom ActionResult

Create a custome ActionResult to return XML [DotNetCurry]

Published in 2011. Malcolm Sheridan writes about how to create a custom ActionResult to return XML. Although this is a very short piece, the entire source code is available for download. Because it’s short, it’s also tightly focused on the specific function of this custom ActionResult.

Maarten Balliauw – Custom ActionResult (ImageResult) [Blog]

Published in 2008. This post provides all the code needed to create this specific custom ActionResult, and breaks the steps down to creating HTMLHelper extension methods, creating the custom ActionResult class, and adding the appropriate action on the HomeController.

Siim Viikman – Simplying MVC controllers with custome ActionResults [Blog]

Published in 2011. Aiming to separate reusable execution logic from base controllers (for cleaner, less repetitive code), this post documents an approach that results in more readable code and improved testability. The introduction is a little long-winded, but each part of the process is discussed documented.

Error Handling

Vijaya Anand – Exception Handling in MVC [Blog]

Published in 2012. This is a thorough, well-structured article on how to tackle Error Handling with the tools already built into MVC. Very readable, and an excellent place to start learning (Also cross-posted to The Code Project where there is some recent discussion.)

MVC Tutorial – Handling Errors and Exceptions [Dev Pro]

Published in 2012. A very thorough article by Dino Esposito describing some best practices for handing both errors and exceptions. The code is a little hard to read because of how DevProConnetions renders it on the page, but the article is definitely still reading.

How to handle errors in MVC [.NET Code Geeks]

Published in 2012. This is a code-heavy article, with very little prose explaining what’s being done. This is a solid off-the-shelf generic solution covering a wide variety of errors, with a few brief (but good) pointers at the end.

One Response to “Controllers and Actions”

  1. [...] You can find my article “Controllers and Routers in MVC 3″ published on CodeProject listed under the “Controllers Overview” section in http://webdev.simple-talk.com/controllers-and-actions/ [...]

Let us know what you think!