D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

ASP.NET MVC Framework - Separating Out View - Controller - Model

Thursday, April 24, 2008 8:18 AM

When you create a new project with the MS-MVC Framework, it defaults to giving you a controller, model, and view folders. The recommendation is to keep that structure if you're going to be developing your application out of one project.

But what if you want to split it up into separate projects: one for the views, one for the controllers, and one for the model. This question was raised at my MVC talk on Tuesday at our local user group and we had a bit of theoretical discussion about how yes, it could happen. I decided to quickly give it a try this morning.

First let's look at the Model Library. Nothing really exciting here, just a project that contains some sort of classes. Same as any other class library, so we can move on.

Next we have the Controller Library. You can split out your controllers into a separate library because the MVC framework looks at the compiled objects that are of the System.Web.Mvc.Controller type and not the physical file sitting in the Controller folder. You do have to add a few references to your Controller Library project though:

System.Web.Mvc

Your Model Library

Likewise, your classes will need to import the model library and the System.Web.Mvc namespace (otherwise ReadFromRequest, as an example, won't work).

Finally, the View project (your initial MVC proj) needs to reference the two new assemblies obviously. One tweak I found was that if you have a View that specifies the type of object passed as view data, you need to set that in both the code behind and also add an imports statement to the source of your aspx (assuming you have code in your source that uses the model objects).

I'm going to look at doing this in a bit more detail...see what else pops up when developing in this tiered way, but for now this is a good primer if you want to try structuring your MVC projects differently than the default.

D




Feedback

# re: ASP.NET MVC Framework - Separating Out View - Controller - Model

Outstanding. Thanks for this. I am currently reading throught the MVC source code and ramping up for a VERY large MVC project. This will come in WAY handy. Thanks again!
~L 4/24/2008 8:41 AM | Lee

# re: ASP.NET MVC Framework - Separating Out View - Controller - Model

Thank you bro.. 9/29/2008 2:20 PM | Sesli Sohbet

# re: ASP.NET MVC Framework - Separating Out View - Controller - Model

THANK YOU.. ADMİN 8/25/2010 6:43 PM | Sesli Sohbet

# re: ASP.NET MVC Framework - Separating Out View - Controller - Model

Thank you so much admin.. 10/30/2010 3:49 AM | Sesli

# SesliEsEs , Sesli EsEs , Eskişehirspor , SesliEsEsLi , Sesli Sohbet , Sesli Chat , Speak Chat , Görüntülü Sohbet , Görüntülü Ch

THANK YOU.. ADMİN . . . . 10/5/2011 6:25 AM | seslisohbet

# re: ASP.NET MVC Framework - Separating Out View - Controller - Model

Admin Thank You . . . 10/21/2011 2:08 PM | sesli

Post a comment