3

How to register Controller in MVC3?

I have 2 solutions:

  • MvcApplication (MVC 3 Web Application) with file 'HomeController.cs'

  • CustomController (Class library) with file 'CustomController.cs'

If i Build 'CustomController' solution and Add reference in MvcApplication solution to this, i can simple use CustomController in application. But how i can do it without 'Add reference' action, just with Assembly.LoadFrom(PathToDll) e.t.c.?

5
  • 1
    Why don't you want to have to Add Reference... for your main project solution? Commented Nov 26, 2011 at 15:03
  • Is it possible to make 'Add Reference' action when i've already published my project and load it to server folder? Commented Nov 26, 2011 at 15:06
  • Are you not able to Add Reference and then republish the project?? Down time will be minimal, and you won't have to worry about this workaround in the future. Commented Nov 26, 2011 at 15:10
  • I'd like to let users upload plugins into my project when it's already compiled and loaded to server in inetpub. Commented Nov 26, 2011 at 15:17
  • Oh, I see. Then it looks like wnascimento's answer may be best. Commented Nov 26, 2011 at 15:19

1 Answer 1

3

You could use MEF for that.

See a good article

http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-(MEF).aspx

This section in your global.asax, register the new controller fac and set the "Plugins" folder for MEF load their libraries. Plugins folder is in your asp.net app root folder.

ControllerBuilder.Current.SetControllerFactory( 
new MefControllerFactory( 
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins")));
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure it should work, but let's see. this.catalog = new DirectoryCatalog(pluginPath); this.container = new CompositionContainer(catalog);
With MEF you can enable the use of plugins. pluginPath is the path where the MEF should seek their compiled libraries(dll). (mvc_app_folder/plugins) for example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.