【问题标题】:How to use a controller in another assembly in ASP.NET Core 3.0?如何在 ASP.NET Core 3.0 的另一个程序集中使用控制器?
【发布时间】:2020-02-28 23:19:19
【问题描述】:

默认的 dotnet core 3 web api 模板假定控制器与 Startup.cs 位于同一程序集中

如何让它知道不同程序集中的控制器?

我个人喜欢让我的解决方案更加分层,并且只取决于它需要依赖的内容

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

所以在MyApp.Host 中,我不希望对 MVC 框架有任何直接依赖(尽管我知道在 Dotnet Core 3 中这已经是隐含的)。控制器在MyApp.WebApi

【问题讨论】:

    标签: asp.net-mvc asp.net-core-3.0


    【解决方案1】:

    解决方法是.AddApplicationPart(assembly)在Startup.cs中添加控制器时。

    所以如果我有一个项目 MyApp.WebApi 依赖于 nuGet 包:Microsoft.AspNetCore.Mvc.Core(当前版本为 2.2.5) 使用以下控制器:

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

    我在MyApp.Host 中有我的Startup.cs:

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

    然后应用程序将检测到不同程序集中的控制器,我将能够访问:https://localhost:5001/samples

    【讨论】:

    • 我还不知道为什么没有 MVC 3.0
    • 更简单的调用是:AddApplicationPart(typeof(SamplesController).Assembly)。您不必调用 Assembly.GetAssembly()。
    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2016-04-30
    • 2018-01-10
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多