gpt4 book ai didi

c# - ASP.NET Core MVC 主项目无法在单独的程序集中访问 Controller

转载 作者:行者123 更新时间:2023-11-30 14:06:46 25 4
gpt4 key购买 nike

我想使用一个单独的项目来存储我的测试应用程序的 Controller 。由于 ASP.NET Core 的工作方式,您需要使用要添加的程序集调用 services.AddMvc().AddApplicationPart

我使用 Visual Studio 2017(因此不再有 project.json)

问题是我无法访问我想要包含的程序集!

我在我的项目中添加了引用:

enter image description here

此外,我决定像这样为 AppDomian 使用 polyfill(以达到我需要的组装):

 public class AppDomain
{
public static AppDomain CurrentDomain { get; private set; }

static AppDomain()
{
CurrentDomain = new AppDomain();
}

public Assembly[] GetAssemblies()
{
var assemblies = new List<Assembly>();
var dependencies = DependencyContext.Default.RuntimeLibraries;
foreach (var library in dependencies)
{
if (IsCandidateCompilationLibrary(library))
{
var assembly = Assembly.Load(new AssemblyName(library.Name));
assemblies.Add(assembly);
}
}
return assemblies.ToArray();
}

private static bool IsCandidateCompilationLibrary(RuntimeLibrary compilationLibrary)
{
return compilationLibrary.Name == ("TrainDiary")
|| compilationLibrary.Dependencies.Any(d => d.Name.StartsWith("TrainDiary"));
}
}

但是当我使用它时,列表中只有一个程序集(只链接到这个项目本身,没有链接到 Controller )。

这是我的Startup.cs:

public class Startup
{

public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var assemblies = GetAssemblies(); // I tought I will find there Assmbly I need but no

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddReact();
services.AddMvc();
services.AddLogging();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseReact(config =>
{
});

app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}

public List<Assembly> GetAssemblies()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
List<Assembly> currentAssemblies = new List<Assembly>();

foreach (Assembly assembly in assemblies)
{
if (assembly.GetName().Name.Contains("TrainDiary"))
{
currentAssemblies.Add(assembly);
}
}

return currentAssemblies;
}

}

如何让我的项目也查找其他项目中的 Controller ?为什么它看不到呢?

UPD

试图像这样直接制作 exapmle在这里。

所以我的代码开始是这样的:

var controllersAssembly = Assembly.Load(new AssemblyName("TrainDiary.Controllers")); 
services.AddMvc()
.AddApplicationPart(controllersAssembly)
.AddControllersAsServices();

并且成功获取到程序集:

enter image description here

但是当我尝试调用 Controller 函数时 - 它失败了!

Controller 代码:

[Route("Login")]
public class LoginController: Controller
{
[Route("Login/Test")]
public void Test(string name, string password)
{
string username = name;
string pass = password;
}
}

要求:

enter image description here

UPD2:

出于某种原因,删除 Routes 有帮助:

public class LoginController: Controller
{
public void Test(string name, string password)
{
string username = name;
string pass = password;
}
}

enter image description here

最佳答案

所以,如果总结一下:

1) 您需要使用 Add Reference 到您的独立项目 Controller 。

2)配置服务(部分):

public void ConfigureServices(IServiceCollection services)
{
var controllersAssembly = Assembly.Load(newAssemblyName("SomeProject.MeetTheControllers"));

services.AddMvc().AddApplicationPart(controllersAssembly).AddControllersAsServices();
}

3) 我的配置看起来像这样(注意 UseMvcWithDefaultRoute):

   // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseReact(config =>
{
});

app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}

所以在我这样的 Controller 中,你不需要在这里放置 Routes 属性,只需通过 Some/Test url 调用它

namespace SomeProject.MeetTheControllers.Test
{
using Microsoft.AspNetCore.Mvc;

public class SomeController: Controller
{
public void Test(string name, string notname)
{
string username = name;
string nan = notname;
}
}
}

关于c# - ASP.NET Core MVC 主项目无法在单独的程序集中访问 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com