1

I am building a UI where there are two different behaviors possible based on some configuration. I want my controllers to be loaded dynamically from different Net core assemblies based on a property value "ProductType" - G or P in appsettings.json

appsetings.json

"ProductType" : "G",

In Startup.cs, on reading the value of property "ProductType" I am loading the corresponding assembly to register the controllers only from that library.

Startup.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")))
}

Both "GLibrary" and "PLibrary" has a controller/action named as Security/Login but with different implementations.

SecurityController.cs

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

project.json contains entry for both libraries.

project.json

"GLibrary"
"PLibrary"

Now on hitting the Security\Login I am getting this error

An unhandled exception occurred while processing the request.
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

GLibrary.Controllers.SecurityController.Login (GLibrary)
PLibrary.Controllers.SecurityController.Login (PLibrary)

How can I avoid this AmbiguousActionException?

harshit
  • 183
  • 2
  • 16

1 Answers1

2

In Configure method, you can use your route customized for each assembly with defining 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" });
        });
    } 
}
Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • Routing namespaces are not available in .Net Core right ? This is not working. – harshit Mar 14 '17 at 05:49
  • yeah. you are right. it's not in core. I have been using it in mvc5. Nevertheless, you can use action constraint by overriding ActionMethodSelectorAttribute attribute. You can do like this : http://stackoverflow.com/questions/34306891/restrict-route-to-controller-namespace-in-asp-net-core – Akash KC Mar 14 '17 at 06:02