c#


Conflicting Controller actions in Asp Net Core


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?
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" });
});
}
}

Related Links

Office (Excel) COM interop on Mac OSX using .NET Core?
C# UWP show messageDialog every given time
Entity Framework auto generate primary key
How can I find out if an object is a List of any type?
Why do decimals give a compile time error on division by zero? [duplicate]
C# - sending, reciving and then sending again via a socket
c# long line of code formatting
XmlDatagrid option not available in Dataset WPF
How to list all subdocuments of a mongodb document in c#
Select from list where not appear in second list
Loading a Dictionary of Objects from a binary file c#
c# Textchanged event on Asp.net is firing twice
Update data using SQL parameter in C# with class
Value not set in custom renderer, when using different native control
How to access array defined in class in another class without constructor?
Convert special characters in string to Unicode Hex Code?

Categories

HOME
autohotkey
excel-formula
swift3
system-calls
typo3
liquibase
x86-64
assign
localhost
browser-cache
qt4
obd-ii
soap-client
google-my-business-api
ldap-query
dompdf
novacode-docx
accordion
aws-devicefarm
include-path
fibonacci
uipath
datastax-enterprise
libxml2
setup-project
dynamics-ax-2012-r3
r-factor
favorites
voyager
minitest
repeat
rythm
scip
mongoid5
pygraphviz
scalamock
ng-lightning
ui-grid
delphi-xe
android-graphview
azure-data-catalog
reqif
npoco
anonymous-function
g77
import.io
sharpdevelop
webmatrix
pyautogui
directinput
mavlink
avaudiorecorder
3d-rendering
innertext
trendline
music-player
fadein
yfiles
producer-consumer
taverna
lytro
log-analysis
stderr
strcpy
chunks
random-access
react-os
sendy
power-management
grunt-wiredep
xrandr
leap-year
codeigniter-form-helper
reference-manual
nsregularexpression
orchestration
m3u
big-endian
filedialog
sqlbuddy
pinchzoom
inserthtml
ruby-1.9
maven-eclipse-plugin
cgaffinetransform
cherokee
isgl3d
symfony-1.2
object-tag
llblgen
linq-to-mysql
qprocess
versions
rpxnow
ffdshow
j++

Resources

Encrypt Message