How can I create dynamic API's in ASP.net Core 2

Viewed 5174

I need to know how to create API's dynamically by calling another API then passing [API name] and [API parameters], let's we assume that we have this API, like the one shown here:

Calling API

[HttpPost("GenerateApi")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public IActionResult GenerateApi(RootObject ro)
{           
   //Here I need piece of code to create API dynamically
   return Ok(new { ro.apiName, ro.parameters });       
}

Here are the models:

public class RootObject
{
   public string apiName { get; set; }
   public List<parameter> parameters { get; set; }
}

public class parameter
{
   public string parameterName { get; set; }
   public dynamic parameterType { get; set; }
}

Now what needs to be done, is to create dynamic API in controller.

So then we would have the API created they can be called from their URL like this

www.example.com/api/v1/[controller]/[apiName]/{[parameter1_value]}/{[parameter2_value]}

Can you please provide me some insight of this.

1 Answers

Found some thing can help in MVC. In case somebody else is looking for this, I need to overwrite the DefaultHttpControllerSelector. Here is a very nice article on the subject: link So basically for my use case mentioned above, I need to create a new AppDomain, start my service in it, and load my assemblies dynamically at runtime. I finally need to overwrite the DefaultHttpControllerSelector to catch the request. When the request arrive, it have then control on which controller I want to use. There I can update the controller dynamically by loading a new assembly, etc. Main thing to be careful on is that this is executed for each request so it could easily impact performance and memory. So I will implement my own caching of controller.

Related