简体   繁体   中英

How do I use a Controller which is in a Class Library?

I have controllers in a class library but I can't work out how to get the main project to recognise them. The main project has a reference to my class library. Do I need to register them somewhere?

I want to use both Controllers and ApiControllers.

EDIT:

Route config - unchanged from creating the project:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Page", action = "Dashboard", id = UrlParameter.Optional }
        );
    }
}

WebApi config, again unchanged:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Controller I'm attempting to get working first:

public class UIController : Controller
{
    [ChildActionOnly]
    public PartialViewResult StartMenu()
    {
        StartMenu menu = StartMenuFactory.Get();

        return PartialView(menu);
    }

    [ChildActionOnly]
    public PartialViewResult Explorer()
    {
        return PartialView();
    }

    //
    // GET: /Page/
    public ActionResult Test()
    {
        return View();
    }
}

I created a Test.cshtml within a UI folder inside Views in my main project. Explorer.cshtml and StartMenu.cshtml are within Shared inside Views.

Does your controller class name end with "Controller", this is mandatory.

What you are doing should work, the controller factory does this:

... the default factory uses a type cache internally. The type cache is implemented in the ControllerTypeCache class. During the application initialization, the ControllerTypeCache class uses .NET reflection to enumerate all the referenced assemblies and explores them looking for publicly exposed controller types. A controller type is any referenced type that passes the following test:

static bool IsControllerType(Type t)
{
     return
        t != null &&
        t.IsPublic &&
        t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
        !t.IsAbstract &&
        typeof(IController).IsAssignableFrom(t);
}

From: https://www.simple-talk.com/dotnet/asp.net/asp.net-mvc-controllers-and-conventions/

This means that a simple reference to an assembly with your controllers should suffice.

If you controller satisfies these rules you should be ok.

The solution for the API controllers is going to be different from the solution for regular MVC due to a number of factors. See the following for information on how to handle Web API controllers: using external web api controllers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM