您不能重定向到不同的 Web API 端点,但您可以执行以下操作之一:
- 用户请求“/a”,您给他一个响应,其中包含指向“/b”或“/c”的 URL/URL。
- 您已经实现了一项服务,并且您的控制器在逻辑上是干净的。因此,在控制器中,您只需检查应该调用“/a”、“/b”还是“/c”的服务方法。
编辑3:
我强烈推荐选项 2,因为您提到您有 3 个具有相同输入数据的控制器操作。因此,最好的方法是只有一个操作,并在其中调用您的服务方法A、B 或C,具体取决于输入数据。
编辑2:
第一个选项伪代码:
[HttpPost("/a")]
public ActionResult<DefaultResponseModel> Verify(MyInputModel myModel)
{
MyResponse response = new MyResponse();
if(myModel.MyProperty1 == "something")
{
response.RedirectURL = "https://myurl/a"
}
else if(myModel.MyProperty1 == "other")
{
response.RedirectURL = "https://myurl/b"
}
else
{
response.RedirectURL = "https://myurl/c"
}
return Ok(respomse);
}
编辑:
如何执行第二个选项的示例。
在ConfigureServices 方法中的Startup.cs 文件中,您可以像这样注册一个服务:
services.AddTransient<IMyService, MyService>();
在你的控制器中,你通过依赖注入来注入它:
public class BusinessController : ControllerBase
{
private readonly IMyService businessService;
public BusinessController(IMyService businessService)
{
this.businessService = businessService;
}
// the actions are below..
// code here
}
然后让我们在你想要的操作中说“/a”
[HttpPost("/a")]
public ActionResult<DefaultResponseModel> Verify(MyInputModel myModel)
{
if(myModel.MyProperty1 == "something")
{
var response = this.businessService.A(myModel)
}
else if(myModel.MyProperty1 == "other")
{
var response = this.businessService.B(myModel)
}
else
{
var response = this.businessService.C(myModel)
}
}
别忘了创建服务和实现。
public interface IMyService
{
ResponseModel A(MyInputModel myModel);
ResponseModel B(MyInputModel myModel);
ResponseModel C(MyInputModel myModel);
}
以及服务的实现:
public class MyService : IMyService
{
enter code here
}