Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate model, view and controller in an ASP.NET MVC app into different assemblies

At the moment I am trying to get into the ASP.NET MVC framework.
For most of my test applications I used a single assembly/project. This worked fine for some smaller applications. Then I wondered how I could place my model, controller and view classes into separate assemblies? In really big web-applications it is not very realistic to put everything into a single assembly/project.

So my question is: Is it possible to tell the ASP.NET MVC framework to search in another assembly for views and/or controllers without losing the built-in flexibility of the routing engine?

like image 777
Alexander Avatar asked May 26 '09 10:05

Alexander


2 Answers

(unknown) is correct. Create two projects, a class library and an MVC web project. The MVC project should reference the class library that contains the controllers and code behind files (global asax etc). Here is an example layout.

The class library should only contain .cs files and no views (.aspx/.ascx files).

MyProject.BaseSite (class library)
    + Controllers
        - HomeController.cs
        - ... any other controllers
    - default.aspx.cs
    - global.asax.cs

MVC web project should contain configs, views etc and a reference to your class library

MyProject.ExampleSite
    + Content
        + scripts
        + css
        + images
    + Views
        + Home
            - index.aspx
            - .. other aspx files
        + Shared
            - Site.master
    - web.config

Remember the different namespaces. You can then create multiple Example websites that reference the same code. This allows you to effectively skin your website completely differently.

like image 103
David Avatar answered Oct 12 '22 15:10

David


Create a separate class library project for each layer of responsibility, compile to create the assembly and then reference each in your application where appropriate.

like image 41
James Avatar answered Oct 12 '22 13:10

James