tags:

views:

553

answers:

6

Hi, I'm using VS2008 and .net 3.5. I have created a class library(Myproject.Controllers) in my solution. Under this class, I have added a Controllers folder. And in the folder I have added a MyController which is declared as public class MyController : Controller

My views are still in the default Views folder. Now, when I run this in VS, I get a message in the Default.aspx.cs: {"The controller for path '/' could not be found or it does not implement IController."}

If I put a copy of my MyController in the default Controllers folder then it works fine. Does anyone know how I can set/configure the Controllers path? I've searched the web and didn't find anything for this. Thank you.

A: 

I did this not too long ago, and the only tricky part was to make sure that the namespace where the controller resides is the same as it would be if it was in the default folder. The main pitfall is the root namespace of the class library project - you can change it by right-clicking the project node in Solution Explorer, selecting Properties and changing the value in the Root Namespace textbox. The easiest way is to name it the same as the MVC application itself.

Tomas Lycken
A: 

From my understanding, this should just work. Did you add your Controllers library as a reference to your web site?

From MVC source

// ControllerTypeCache.cs:
private static List<Type> GetAllControllerTypes(IBuildManager buildManager) {
    // Go through all assemblies referenced by the application and search for
    // controllers and controller factories.

// DefaultControllerFactory.cs
// if all else fails, search every namespace
return GetControllerTypeWithinNamespaces(controllerName, null /* namespaces */);
Talljoe
A: 

Hi, Tomas, my class library where the MyController resides has the same namespace as if I were to add a controller in the default Controller folder. The reason is my class libray name is MyProject.Controllers so it produce the same namespace as the default.

Hi Talijoe, Where do I add the controllers library as a reference to my web site? I assume the cod you show is the actual code I would add, but where or what file do I add them?

Thank you.

A: 

Hi Talijoe, sorry I just saw the file names where to add these code are ControllerTypeCache.cs and DefaultControllerFactory.cs

Where are these 2 files that i need to modify? Thank you.

Also, I check to be notify by email when there is reply posted to my question on this page but there is no submit button to send this. How do I send this request?

You can add comments directly to the question by clicking the "add comment" button.The filenames I referenced are code from ASP.NET MVC, nothing you need to touch. I was pointing out that what you are trying to do should work. You just need to make sure you add a reference to your controllers project from your website project.
Talljoe
A: 

I had a similar issue using dotnet 4.0 utilizing MVC 2.0 on IIS7.0. After checking all the normal stuff, having the right MVC assembly version and so on, i finally moved the code from a random location on the web server to /inetpub/www and recreated my site on the webserver. That seemed to have done the trick. Aparently, DotNet MVC likes to have the source under /inetpub/www to be able to resolve the MVC paths.

shafi jourabchi
+1  A: 

By creating your own Controller factory, you can specify exactly how controllers are used. Here's how

step 1 - create a class and derive it from IControllerFactory

http://msdn.microsoft.com/en-us/library/system.web.mvc.icontrollerfactory_members.aspx

step 2 - Consume your new Controller Factory in application start

ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory));

IControllerFacotry implements 2 methods

IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName);
void IControllerFactory.ReleaseController(IController controller);

And that's all there is to it.

Seattle Leonard