5

I've got two actions in my ASP.NET Core Web API application's controller:

[HttpGet("get-all")]
public IActionResult GetAll() { ... }

and

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
    ...

    return RedirectToAction("GetAll"); 
}

Delete action always redirects to itself and never to GetAll. Why so? In the same time similar redirect from Post action works ok.

Can't find any docs on the subject. Any help?

7
  • Why would you redirect in a web api? Commented May 25, 2018 at 23:41
  • You can make a request in Delete action to GetAll action by HttpClient's GetAsync method and return it's result. Commented May 26, 2018 at 7:59
  • @Brad, I just want to return updated collection to the client without issuing a new request to GetAll. Commented May 26, 2018 at 8:04
  • @Orhun, RedirectToAction method is meant to do the thing, isn't it? Commented May 26, 2018 at 8:06
  • 1
    The DRY solution is for your delete action to return NoContent() and then let the client request GetAll(). Commented May 26, 2018 at 13:36

1 Answer 1

3

Have you tried to use RedirectToActionResult? Like this (change ControllerName with your actual Controller's name ):

RedirectToActionResult("GetAll", "ControllerName", null);

Documentation

Hope you'll find this useful.

Sign up to request clarification or add additional context in comments.

2 Comments

You can Aldo try to RedirectToRoute("routeName", routeValues) where routeName can be Added to the http route as a name e.g. [HttpGet("get-all", Name = "routeName")]
What is the difference between ReDirectToAction & ReDirectToActionResult?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.