简体   繁体   English

如何通过Nuget软件包中的.NET Core扩展添加路由或视图?

[英]How to add routes or views by .NET Core extension packed in Nuget package?

I am just about to build an app and it is really likely I will use big portion of the code again and again for other customers. 我将要构建一个应用程序,确实很有可能我会一遍又一遍地为其他客户使用大部分代码。

The best way to manage the code in one place and also be able to update applications is to have it in private Nuget package. 在一处管理代码并能够更新应用程序的最佳方法是将其放在私有的Nuget软件包中。

To make it happen I would need to be able to use MVC in the controllers, eg [HttpGet] etc. I understand how I can add all my services and make them available in application like singletons etc, but I cannot find anything about the way how I could add the endpoints or views this way? 为了实现它,我需要能够在控制器中使用MVC,例如[HttpGet]等。我了解如何添加我的所有服务并使它们在应用程序中(例如单例)可用,但是我找不到关于此方式的任何信息如何以这种方式添加端点或视图?

Is it possible at all to attach all my controllers etc by calling eg services.AddMyModule() ? 是否可以通过调用例如services.AddMyModule()来附加我所有的控制器等?

This is covered by the application parts documentation chapter. 应用程序零件文档一章对此进行了介绍。 Basically, by default, ASP.NET Core will automatically pick up controller from other assemblies as long as they are referenced within your project. 基本上,默认情况下,只要在项目中引用了其他程序集,ASP.NET Core就会自动从其他程序集中拾取控制器。

So for your case, if a project depends on that NuGet package, and the NuGet package contains controllers and other application parts, then it should be automatically picked up: 因此,对于您的情况,如果一个项目依赖于该NuGet包,并且该NuGet包包含控制器和其他应用程序部件,则应自动将其拾取:

By default MVC will search the dependency tree and find controllers (even in other assemblies). 默认情况下,MVC将搜索依赖关系树并查找控制器(甚至在其他程序集中)。 To load an arbitrary assembly (for instance, from a plugin that isn't referenced at compile time), you can use an application part. 要加载任意程序集(例如,从编译时未引用的插件),您可以使用应用程序部件。

If you need more control over this and the default doesn't work for you, you can still add assemblies as application parts explicitly like this: 如果您需要对此进行更多控制,并且默认设置不适合您,您仍然可以像这样明确地将程序集添加为应用程序部件:

services.AddMvc()
    .AddApplicationPart(typeof(SomeTypeInThatAssembly).Assembly);

And you can also remove assemblies explicitly if you don't want the default behavior: 如果您不想使用默认行为,也可以显式删除程序集:

services.AddMvc()
    .ConfigureApplicationPartManager(m =>
    {
        var part = m.ApplicationParts.FirstOrDefault(p => p.Name == "AssemblyNameToRemove");

        if (part != null)
            m.ApplicationParts.Remove(part);
    });

So basically, if you (and your users) can live with the default behavior, you can just define your controllers in that assembly and you're good to go. 因此,基本上,如果您(和您的用户)可以使用默认行为,则只需在该程序集中定义控制器就可以了。 Remember that for views to be included in the assembly, you need to make sure that view compilation is enabled. 请记住,要使视图包含在装配中,您需要确保已启用视图编译

You want AddControllersAsServices . 您需要AddControllersAsServices This coupled with AddApplicationPart will let you add them out of an assembly and into the current MVC instance. 结合使用AddApplicationPart您可以将它们从程序集中添加到当前MVC实例中。 My code looks like 我的代码看起来像

services.AddMvc()
    .AddApplicationPart(typeof(MyProject.MyNamespace.Controllers.MyController).GetTypeInfo().Assembly)
    .AddControllersAsServices();

Where services is my IServiceCollection . services是我的IServiceCollection

If you want to have something like services.AddMyModule() you'll need a static extension method that takes an IMvcBuilder and calls the above code. 如果要使用services.AddMyModule()类的东西,则需要一个静态扩展方法,该方法需要一个IMvcBuilder并调用上面的代码。 Something like: 就像是:

public static void AddMyModule(this IMvcBuilder build)
{
    services.AddMvc().AddApplicationPart(typeof(MyProject.MyNamespace.Controllers.MyController).GetTypeInfo().Assembly)
}

Edit: As poke points out, AddControllersAsServices can have wider ranging effects than just adding the controllers in the list. 编辑:正如戳所指出的那样, AddControllersAsServices可以具有比仅在列表中添加控制器更广泛的影响范围。 So I've removed it from my answer. 因此,我已将其从答案中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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