skip to Main Content

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);
}

And:

[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!

2

Answers


  1. Apply the following Route attributes:

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

    For more information see Routing in ASP.NET Core

    Login or Signup to reply.
  2. just fix the action route

    [HttpGet("{id?}")]
    public IActionResult Assign(int? id)
    {
        return View("Create", id);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search