如何使用类库中的控制器? [英] How do I use a Controller which is in a Class Library?

查看:330
本文介绍了如何使用类库中的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类库中有控制器,但无法弄清楚如何使主项目识别它们.主要项目有对我的班级图书馆的引用.我需要在某个地方注册吗?

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?

我想同时使用Controllers和ApiControllers.

I want to use both Controllers and ApiControllers.

路由配置-与创建项目相同:

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配置,再次保持不变:

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();
    }
}

我在主项目的Views内的UI文件夹内创建了Test.cshtml. Explorer.cshtml和StartMenu.cshtml位于视图内的共享"中.

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.

推荐答案

您的控制器类名称是否以"Controller"结尾,这是强制性的.

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

您正在执行的操作应该可以正常工作,控制器工厂可以执行以下操作:

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

...默认工厂使用类型缓存 内部.类型缓存是在 ControllerTypeCache类.在应用程序初始化期间, ControllerTypeCache类使用.NET反射来枚举所有 引用程序集并对其进行探索,以查找公开显示的程序集 控制器类型.控制器类型是任何传递的引用类型 以下测试:

... 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);
}

来自: 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.

这篇关于如何使用类库中的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆