Asp.net Web Api as Api Gateway

In my organization, I have several asp.net web api projects.
I am ready to develop a web api that will act as a gateway and have a single entry point.

I tried handlers and filters, but routing does not allow me to do this.

Let me explain what I really want to do.

Let HumanResourceProject at: api.domain.com/api/hr Sample Controller be api / hr / LeaveRecord / GetByEmployee /
Let CRMProject at: api.domain.com/api/crm Sample Controller - api / crm / Clients / GetByRegion /
Let Gateway at: api.domain.com/api/gateway

I want my gateway to process requests, for example, 1. api.domain.com/api/gateway/ api / crm / Customers / GetByRegion /
or
api.domain.com/api/gateway/ api / hr / LeaveRecord / GetByEmployee /

  1. Do some controls like ip / access / browser etc.

  2. I want to call the correct api controller api / crm / Customers / GetByRegion /

  3. Returns the response of the called api controller.

enter image description here

+8
c # asp.net-web-api
source share
1 answer

OK,

  • First you need to register your web routes. Make sure you map your route format in the WebApiConfig.cs file as follows:

    // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 
  • Create web api controllers and set route prefix

 [RoutePrefix("api/crm")] [System.Web.Http.Cors.EnableCorsAttribute("*","*","*")] [Authorize] public class CRMController: ApiController { 
  1. Create your management methods for your requests.
 [HttpGet] [Route("GetByRegion/{regionId}")] public IHttpActionResult GetSomeDataByRegion(int regionId) { 
  1. I would advise you to test the endpoints with tools like Fiddler or Chrome extensions like Rest Console to make your request to receive / send, etc.

edit: it is always useful to wrap your parameters with a model class if you have an unknown complex object that might cause a problem when executing a query. Sniffing traffic will help you find the problem easier.

-one
source share

All Articles