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

谁都会走 提交于 2019-12-08 19:19:02

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21705229/how-do-i-use-a-controller-which-is-in-a-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!