Anand
Anand

Reputation: 1699

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?

Upvotes: 1

Views: 1006

Answers (2)

Yiyi You
Yiyi You

Reputation: 18209

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")]
        }
    }

Upvotes: 1

Anand
Anand

Reputation: 1699

return Redirect($"rules?businessDate={businessDate:yyyy-MM-dd}&category={category}"); worked.

Upvotes: 0

Related Questions