'How to call HTTP POST method from another Controller which Have a HTTP GET method in WEB API 2
I am working on a project which have scenario to redirect to httppost method from another controller which have a httpget method. Can you guys suggest me a good approach to achieve it?
Example is given below
ControllerA :
[HttpPost]
[Route("Student/InsertData", Name = "InsertData")]
[RequiredQueryStringKeys("SystemId", "IncidentDt")]
public IHttpActionResult InsertData([FromBody] StudentDetailRequest request)
{
//Code
}
Controller B:
[HttpGet]
[Route("Student/GetDetail")]
[RequiredQueryStringKeys("SystemId", "IncidentDt", "Cd")]
public IHttpActionResult GetDetail()
{
------- Need to call ControllerA POST method here
}
NOTE: My requirement is to reroute Student/GetDetail to Student/InsertData. If User hit Student/GetDetail then it should redirect to Student/InsertData.
Solution 1:[1]
If you have problems like this one, then you need to think about refactoring that. For example, you can write some UseCases, and just from your methods call that UseCase scenario. Try to write whole logic out of the controllers, then you will be able to reuse that again, instead of logic inside of methods in controller just try to handle request and call that UseCase what you need.
With this logic, you will also have problems when you want to call that POST method because he required some model, and you don't have it when you first call your GET method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Radovancev |
