Who can help me? using YARP in an ApiController. Thanks Very much! #2322
-
Who can help me? using YARP in an ApiController. Thanks Very much!Who can help me? I need to implement traffic distribution using YARP in an ApiController. The logic for this needs to be written in my own code. Additionally, I need to adapt the parameters sent by users. For example, in an ApiController's Request targets:
Finally, I need to handle the returned result before sending it to the user. ApiController -- need to
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
public TestController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var targetUri = new Uri("https://example111.com/api/test?aaa={Custom parameters}" + HttpContext.Request.Path + HttpContext.Request.QueryString);
// Case 1: https://example111.com/api/test?aaa={Custom parameters}
// Case 2: https://example222.com/api/testTwo?bbb={Custom parameters}
// http IReverseProxyFeature
// var response =
return Ok(response);
}
} ApiController --- by ChatGPT
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
public MyController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id, [FromServices] IReverseProxyFeature reverseProxyFeature)
{
// Dynamically set the target server address based on your logic
var targetServerAddress = "https://example111.com/api/test?aaa={Custom parameters}";
var httpClient = _httpClientFactory.CreateClient();
// Use IReverseProxyFeature to set the target server address
var reverseProxyHttpContextFeature = HttpContext.Features.Get<IReverseProxyFeature>();
if (reverseProxyHttpContextFeature != null)
{
reverseProxyHttpContextFeature.ProxiedDestination = new DestinationState(targetServerAddress);
}
// Make the request
var response = await httpClient.GetAsync($"/api/order?pid=1&t=1699843081{id}");
if (response.IsSuccessStatusCode)
{
// Process the successful response
var content = await response.Content.ReadAsStringAsync();
return Ok(content);
}
else
{
// Handle the error response
return StatusCode((int)response.StatusCode, response.ReasonPhrase);
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Start with the examples here: https://microsoft.github.io/reverse-proxy/articles/direct-forwarding.html While it shows code in Startup and Map, most of that can be moved to a controller as well. Just be careful about the lifetime of the httpClient, you don't want to create a new one per request. You can store it in a static field.
Is this a GET or POST request? Does it already have a Content-Type? You wouldn't usually just add/change the Content-Type header, you'd have to replace the contents of the request body as well. See https://microsoft.github.io/reverse-proxy/articles/transforms.html#request-body-transforms
LOL, none of that is right. I guess my job is safe for another day. |
Beta Was this translation helpful? Give feedback.
Start with the examples here: https://microsoft.github.io/reverse-proxy/articles/direct-forwarding.html
While it shows code in Startup and Map, most of that can be moved to a controller as well. Just be careful about the lifetime of the httpClient, you don't want to create a new one per request. You can store it in a static field.
Is this a GET or POST request? Does it already have a Content-Type? You wouldn't usually just add/change the Content-Type header, you'd have to replace the contents of the request body as well. See https://microsoft.gith…