Search code examples
asp.net-web-apiaspnetboilerplate

How to store and call web API by name?


I want to save the name of API to the database and call it after that. How can i do that?

Example:

I want to save the name of these APIs (Action1, Action2 and Action3) into the database and call it when I need (because I want to reorder or delete some actions in the UI):

public interface IMyAppService : IApplicationService
    {
        Task Action1();
        Task Action2(); 
        Task Action3();          
    }

So how can I store the API name as string in database and how to execute it after that? Should I store them like this?

"http://localhost/api/services/mpa/myApp/Action1"
"http://localhost/api/services/mpa/myApp/Action2"
"http://localhost/api/services/mpa/myApp/Action3"

Solution

  • You have 2 options:

    1. save it by URL + use HttpClient, or

      • Pro: Transaction rollback is handled by ABP framework when an exception is thrown.
      • Con: You need to handle web request, authorization and deserialization of response.
    2. save it by AppService and method names + use reflection.

      • Pro: Authorization is handled by ABP framework when in IAbpSession.Use() block.
      • Con: You may need to handle transactions and exceptions ⇒ logic leaks to the caller.

    For completeness sake, this question is the first of a series of other Stack Overflow questions:

    1. This question. (Oct 23)
    2. How to call Web API (App Service) remotely (Oct 31) — Option 1
    3. How to call web API under specific user permission? (Oct 31) — Option 1, switch to Option 2
    4. How to use reflection to call a API by string name? (Nov 3) — Option 2