简体   繁体   中英

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.

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?

Is it possible at all to attach all my controllers etc by calling eg 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.

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:

By default MVC will search the dependency tree and find controllers (even in other assemblies). 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 . This coupled with AddApplicationPart will let you add them out of an assembly and into the current MVC instance. My code looks like

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

Where services is my 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. 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. So I've removed it from my answer.

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