Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controllers and Views inside a Class Library

Tags:

asp.net-mvc

I've a group of controllers and Views that I use in many projects. I was wondering if I could place then inside a class library and reuse it as a normal lib file. How can I do this?

like image 444
Extralive Avatar asked Nov 25 '09 22:11

Extralive


2 Answers

Definitely doable. Things you'll need:

  1. Some way of registering the routes on app start.
  2. A view engine that knows about both the library views and your application specific views.

The first is easy. Just write a function in your library that registers all the controllers in the library.

The second is more complicated, but you can read some good articles on writing your own view engine, here and here. The concept of Areas may be useful too for separating your library controllers from your app controllers.

Also, this question addresses the simplest case of finding views in an arbitrary folder.

like image 76
Joel Avatar answered Oct 20 '22 00:10

Joel


The controllers can just go into a class library - not much to do there. The views are a bit more difficult. You should be able to embed them in the assembly and implement a VirtualPathProvider to provide access to them.

I haven't tried this, so YMMV.

like image 43
Ryan Emerle Avatar answered Oct 19 '22 23:10

Ryan Emerle