'How to redirect to ASP.NET Web API route in same controller
With 2 routes as follows:
[HttpGet("rules")]
public async Task<IActionResult> GetRulesAsync(DateTime? businessDate = null, string
category = null)
{
//...
}
[HttpGet("effectiveRules")]
public async Task<IActionResult> GetEffectiveRulesAsync(DateTime? businessDate = null, string
category = null)
{
//want to redirect this to [HttpGet("rules")]
}
How can I redirect from second to the first API?
Tried RedirectToRoute un-successfully.
Could anyone please help?
Solution 1:[1]
Here is a working demo to use RedirectToAction:
[Route("[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet("rules")]
public async Task<IActionResult> GetRulesAsync(DateTime? businessDate = null, string
category = null)
{
//...
}
[HttpGet("effectiveRules")]
public async Task<IActionResult> GetEffectiveRulesAsync(DateTime? businessDate = null, string
category = null)
{
return RedirectToAction("rules", new { businessDate = businessDate, category = category });
//want to redirect this to [HttpGet("rules")]
}
}
Solution 2:[2]
return Redirect($"rules?businessDate={businessDate:yyyy-MM-dd}&category={category}"); worked.
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 | Yiyi You |
| Solution 2 | Anand |
