Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
Jon Avatar asked Feb 11 '14 14:02

Jon


People also ask

How do you call a model class in controller class?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.

How do you call a class library in C #?

You can click the Browse tab, locate the folder where the library resides and select it. Call the Library class methods in your application : After selecting the library, you can click OK. You can then use the classes and methods of the library like you would use those of the . NET Framework.

What are controllers used for in C#?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.


1 Answers

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.

like image 77
Rui Avatar answered Oct 13 '22 16:10

Rui