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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

(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.

Method 2

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.

Method 3

Yes, if you use one of the supported dependency injection containers, then their configuration data typically specify not only the class to be loaded in response to a particular query, but also the assembly from which it is to be loaded. This allows you to split your classes up across arbitrary assemblies and MVC will still be able to find them.

Although, of course, the simpler answer provided by Unknown (Google) will also work!

Method 4

I realize that this is a really old question but I have written an article on how to exactly what you are asking for

http://dotnetslackers.com/articles/aspnet/storing-asp-net-mvc-controllers-views-in-separate-assemblies.aspx

Method 5

Only completing @David’s answer:

If your project is managed by NuGet, after creating the class libraries, perform a copy of your packages.config file to the class libraries root. Afterwards, you should edit each packages.config file adding or removing packages, according to the needs of each class library.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x