Answered by:
How can I create a new controller dynamically after building the project?

Question
-
User1052024640 posted
I have made an API controller by asp.net core to communicate with a third-party APP.
When the user authorizes my API in the third-party APP, my API needs to provide a brand new controller and URL to the APP which only works for this user. And then, the server of the APP will transfer data from the user to my API via the URL.
Each controller only differs from some string such as user name and token so that I made a base controller and about to inherit from it.
Whereas, I don't know how to create a controller dynamically after building the project.
In addition, I need the controller which creates dynamically still exists even the server of my API restart.
How can I achieve it? Thank you.
Sunday, March 8, 2020 7:50 AM
Answers
-
User753101303 posted
Or directly as part of the url. I assume you plan to create another controller to have a distinct endpoint for each user?
Instead you could have a single controller and use for example /api/{tenantid}/{controller} so that each company have its own end point. It doesn't prevent to pass as well an API key and/or use authentication.
Else be explicit about the problem you try to solve by creating new controllers but more likely you have a simpler solution.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 9, 2020 11:49 AM
All replies
-
User1120430333 posted
A controller is a class. You can make a dynamic class for special purposes. But I don't think it applies to a class like a controller.
https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject?view=netframework-4.8
Sunday, March 8, 2020 8:12 AM -
User1052024640 posted
What I need to do is not only to create a new class but also to create a new route dynamically. I have to solve the dynamical route also.
Sunday, March 8, 2020 8:15 AM -
User1120430333 posted
Not with C# or VB are you going to be able to do this at runtime.
They are not interpretive languages where you can make a class dynamically at runtimetime
Sunday, March 8, 2020 9:41 AM -
User753101303 posted
Hi,
And how do you pass this extra info? Seems more complex than needed. You can't just include a tenant id for example as part of the url?
Sunday, March 8, 2020 12:26 PM -
User1052024640 posted
You mean just by passing the query string in URL. Right?
Monday, March 9, 2020 1:32 AM -
User1120430333 posted
You mean just by passing the query string in URL. Right?
Do you think it will help you?
https://stackify.com/writing-multitenant-asp-net-core-applications/
Monday, March 9, 2020 2:36 AM -
User753101303 posted
Or directly as part of the url. I assume you plan to create another controller to have a distinct endpoint for each user?
Instead you could have a single controller and use for example /api/{tenantid}/{controller} so that each company have its own end point. It doesn't prevent to pass as well an API key and/or use authentication.
Else be explicit about the problem you try to solve by creating new controllers but more likely you have a simpler solution.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 9, 2020 11:49 AM