Asp Net Core中的控制器操作冲突

时间:2017-03-14 03:02:33

标签: c# asp.net asp.net-mvc model-view-controller asp.net-core-1.0

我正在构建一个UI,根据某些配置,可能存在两种不同的行为。 我希望我的控制器可以根据属性值从不同的Net核心程序集动态加载" ProductType" - appsettings.json

中的G或P.

appsetings.json

Public static Models.InternshipModel mod = new Models.InternshipModel() { Major = "Computer Science", Employer = "Random", Title = "Student" }; 

在Startup.cs中,读取属性的值" ProductType"我正在加载相应的程序集以仅从该库中注册控制器。

Startup.cs

"ProductType" : "G",

两者" GLibrary"和" PLibrary"有一个名为Security / Login但有不同实现的控制器/动作。

SecurityController.cs

string productType = Configuration["ProductType"];
if (productType.Equals("G", StringComparison.OrdinalIgnoreCase))
{
  services.AddMvc()
  .AddApplicationPart(Assembly.Load(new AssemblyName("GLibrary")))
}
else if (productType.Equals("P", StringComparison.OrdinalIgnoreCase))
{
  services.AddMvc()
  .AddApplicationPart(Assembly.Load(new AssemblyName("Plibrary")))
}

project.json包含两个库的条目。

project.json

public IActionResult Login()
    {
            //Unique Implementation
            return View();
        }
    }

现在点击安全\登录我收到此错误

"GLibrary"
"PLibrary"

如何避免此AmbiguousActionException?

1 个答案:

答案 0 :(得分:2)

Configure方法中,您可以使用为每个装配定制的路线,并定义namespace

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    string productType = Configuration["ProductType"];
    if (productType.Equals("G", StringComparison.OrdinalIgnoreCase))
    {
      app.UseMvc(routes =>
        {
            routes.MapRoute(
            name: "default",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional  },
            namespaces: new [] { "GLibrary.Controllers" });
        });
    }
    else if (productType.Equals("P", StringComparison.OrdinalIgnoreCase))
    {
      app.UseMvc(routes =>
        {
            routes.MapRoute(
            name: "default",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional  },
            namespaces: new [] { "PLibrary.Controllers" });
        });
    } 
}