Ask the WebAPI controller to send an HTTP request to another controller within the same service

I have a WebAPI service and I want it to send an HTTP request to itself. I would like to confirm what the most appropriate way to do this looks like. (Usually, I simply created an instance of another instance of the target controller or refactored the interface code and then made the request this way, but for various reasons I do not want to use this method.)

Is the code below the most appropriate way to make an HTTP request a different controller within the same service?

using (HttpClient client = new HttpClient())
{
  var httpRequest = new HttpRequestMessage("GET", "https://localhost/SomeOtherController");
  return await client.SendAsync(httpRequest);
}
+4
source share
2 answers

Yes, that would be a good way to call him.

HTTP-, , - HTTP-.

- , , - URI, .

+2

, , . .

:

public class SomeService
{
 public void DoSomeLogic()
 { 
 }
}

public class FirstController : ApiController
{
 public IHttpActionResult SomeAction()
 {
   new SomeService().DoSomeLogic();
 }
}

public class SecondController : ApiController
{
 public IHttpActionResult SomeOtherAction()
 {
   new SomeService().DoSomeLogic();
 }
}
+4

Source: https://habr.com/ru/post/1660133/


All Articles