如何在 ASP.NET Core 3.0 中的另一个程序集中使用控制器?
How to use a controller in another assembly in ASP.NET Core 3.0?
默认的 dotnet core 3 web api 模板假定控制器与 Startup.cs
在同一个程序集中
如何让它识别不同组件中的控制器?
我个人喜欢让我的解决方案更加分层,并且只取决于它需要依赖的东西
MyApp.Host --> MyApp.WebApi --> MyApp.Application --> MyApp.Domain
所以在 MyApp.Host
中,我不想直接依赖 MVC 框架(尽管我知道在 Dotnet Core 3 中这已经是隐含的)。控制器在 MyApp.WebApi
解决方法是.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;
}
}
}
我的 Startup.cs
在 MyApp.Host
中是:
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
默认的 dotnet core 3 web api 模板假定控制器与 Startup.cs
如何让它识别不同组件中的控制器?
我个人喜欢让我的解决方案更加分层,并且只取决于它需要依赖的东西
MyApp.Host --> MyApp.WebApi --> MyApp.Application --> MyApp.Domain
所以在 MyApp.Host
中,我不想直接依赖 MVC 框架(尽管我知道在 Dotnet Core 3 中这已经是隐含的)。控制器在 MyApp.WebApi
解决方法是.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;
}
}
}
我的 Startup.cs
在 MyApp.Host
中是:
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