12

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?

Venemo
  • 18,515
  • 13
  • 84
  • 125
Alexander
  • 3,724
  • 8
  • 42
  • 50

5 Answers5

6

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

David
  • 15,150
  • 15
  • 61
  • 83
  • I've just tried this and had to have a global.asax in my mvc project that inherits from the one in my class library. Apart from that it looks ok. – Nick Randell Apr 15 '11 at 17:21
  • Is it right to go with the approach of separating them into different assemblies. I know the question asked here is how to do it, so I'm not saying this is a wrong answer. I just want to know when I should consider separating them like that. I was under the impression that this explained in the link here is the right way.. http://stackoverflow.com/questions/8988472/mvc-3-project-structure – user20358 May 31 '12 at 15:42
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.

James
  • 80,725
  • 18
  • 167
  • 237
  • You mean for example instead of using folder "Views" in my main assembly i can simply create a class library named "MyProject.Views"? – Alexander May 26 '09 at 11:17
  • Hmm i tested this, but it doesn't work! The MVC framework will still store everything in the main assembly. – Alexander May 26 '09 at 18:58
  • I mean basically group your code into relevant projects and compile them as separate library classes which would generate an assembly. Then you just reference that assembly in your project where need to access them. I assure you it works... – James May 27 '09 at 09:54
  • I think you are adding classes to your website project and then compiling as a web application....which will indeed generate 1 assembly for all. You need to create separate class library projects and move the classes into each and then compile separately. – James May 27 '09 at 09:56
1

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

Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
  • Dustin: Would you recommend that over MVC Areas. I know what you are sayign is also doable, but I have not seen any MSDN article saying that they support project structures like that. Infact they recommend using Areas to logically partition out different areas of the application... – user20358 May 31 '12 at 10:51
  • @user20358 The article was written for MVC2 (which supported areas). There might be better ways of doing this in MVC3/4, I'm not sure. Areas did not meet my specific needs since they were contained in the same project and I needed them to be in different projects. I cannot recommend one approach over the other, it depends on your needs. – Dustin Davis May 31 '12 at 13:57
  • ok. so what could be a good reason to separate the controllers from the views.. as in have controllers in one project and views in another..Im not questioning your approach but just trying to educate myself on what to use in future projects if I have similar needs.. here is a question I asked earlier where the posts strongly suggested going with areas. http://stackoverflow.com/questions/8988472/mvc-3-project-structure Thanks for yourreply. – user20358 May 31 '12 at 15:39
  • @user20358 my reason was because I was building a CMS. The goal was reuse of controllers and views by other projects. I'm not going to suggest this method is better than XYZ, it depends on your needs. – Dustin Davis May 31 '12 at 19:44
1

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!

Colin Desmond
  • 4,824
  • 4
  • 46
  • 67
0

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.

Leonel Sanches da Silva
  • 6,972
  • 9
  • 46
  • 66