Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a controller in another assembly in ASP.NET Core 3.0?

The default dotnet core 3 web api template assumes the controllers are in the same assembly as the Startup.cs

How to make it aware of the controllers in a different assembly?

I personally like having my solutions more layered and depending only on what it needs to depend

MyApp.Host --> MyApp.WebApi --> MyApp.Application --> MyApp.Domain

So in MyApp.Host I don't want any direct dependency to MVC framework (although I know that in Dotnet Core 3 this is already implicit). The controllers are in MyApp.WebApi

like image 799
diegosasw Avatar asked Nov 03 '19 11:11

diegosasw


People also ask

How do you call a controller action from another controller in Web API?

I'm assuming you want to return that action's result. var ctrl= new MyController(); ctrl. ControllerContext = ControllerContext; //call action return ctrl. Action();

What is the difference between controller and ControllerBase?

Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. If the same controller must support views and web APIs, derive from Controller . The following table contains examples of methods in ControllerBase .


1 Answers

The solution is to .AddApplicationPart(assembly) when adding the controllers in Startup.cs.

So if I have a project MyApp.WebApi that has a dependency to the nuGet package: Microsoft.AspNetCore.Mvc.Core (current version is 2.2.5) with the following controller:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace MyApp.WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SamplesController 
        : ControllerBase
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            var samples =
                new List<string>
                {
                    "sample1", "sample2", "sample3"
                };

            return samples;
        }
    }
}

and I have my Startup.cs in MyApp.Host as:

using MyApp.WebApi.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;

namespace MyApp.Cmd.Host
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var sampleAssembly = Assembly.GetAssembly(typeof(SamplesController));
            services
                .AddControllers()
                .AddApplicationPart(sampleAssembly)
                .AddControllersAsServices();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

then the app will detect the controllers in different assembly and I'll be able to access: https://localhost:5001/samples

like image 135
diegosasw Avatar answered Oct 18 '22 18:10

diegosasw