Base solution for your next web application
Open Closed

Make API Parameters based on Routing #1505


User avatar
0
ofir1234 created

Hi. We are using ABP for a website which is divided to three zones: red zone, green zone, yellow zone. In the server side we want to know for each api call, from which zone it came from. Our solution now is just sending the zone as a parameter for each api call. What we want to do is to use routing for that. Is there a way changing the routing somehow to support this idea? For example, all calls coming to /app/services/red/someAppServicewill/someMethod will actually go to the someMethod in the someAppService, and the parameter "zone" will be set to red.

Thanks in advance.


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    It can be possible. We define the route here: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Api/WebApi/AbpWebApiModule.cs#L118">https://github.com/aspnetboilerplate/as ... le.cs#L118</a>

    So, you can try to add similar routes like (example for red):

    httpConfiguration.Routes.MapHttpRoute(
                    name: "AbpDynamicWebApiRed",
                    routeTemplate: "api/services/red/{*serviceNameWithAction}",
                    defaults: new { zone = "red" }
                    );
    

    This solves the routing issues. Then we should get the 'red' information. We have 2 alternatives:

    • We can try to create an interceptor for IApplicationService interfaces in the Web Api project. See this article to create an interceptor: <a class="postlink" href="http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit">http://www.codeproject.com/Articles/108 ... eptors-wit</a>
    • We can write a Web API action filter.

    In this interceptor/filter, we can set Zone property from the route value. I suggest you to define a special interface, like IHasZone, which defines a Zone property. Thus you can check if input DTO implements IHasZone, cast to IHasZone and set the Zone property.