Home > Software design >  RedirectToAction in same controller not working?
RedirectToAction in same controller not working?

Time:11-06

So I'm trying to use RedirectToAction to redirect to another action in the same controller. However, everything that I have tried has not worked. Ideally I want to be able to type the link /projects/assign/2 and be redirected to the /projects/create page. Examples of what I have tried:

        [HttpGet]
        public IActionResult Assign(int? id)
        {
            return View("Create", id);
        }
        [HttpGet]
        public IActionResult Assign(int? id)
        {
            return View("Create");
        }

When I type /projects/assign/2 in the address bar, I receive an error that says "This page isn't working. If the problem continues, contact the site owner." At this point, I'm not sure what I am doing wrong and how to make it work.

Any help/advice is greatly appreciated! Thank you!

CodePudding user response:

Apply the following Route attributes:

[Route("Create/{id:int?}")]      
[Route("Assign/{id:int?}")] 

public IActionResult Create(int? id)
{
   ...
}
  • Related