RedirectToRoute question

Stesvis 1,041 Reputation points
2021-05-26T18:28:03.337+00:00

I have a project using MVC 5 Web APIs.
In one of my ApiController(s) I need to use the return RedirectToRoute(name, routeValues) method.

The problem I have is that I don't know how to get the name of the destination route . Is there an [Attribute] that allows me to set the name of each route?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chao Deng-MSFT 796 Reputation points
    2021-05-28T07:14:30.777+00:00

1 additional answer

Sort by: Most helpful
  1. Chao Deng-MSFT 796 Reputation points
    2021-05-27T02:41:45.563+00:00

    Hi @Stesvis ,

    You can refer to the demo below.

    Controller:

    public class TestController : Controller  
        {  
            // GET: Test  
            public ActionResult Index()  
            {  
                return View();  
            }  
      
            public ActionResult Test()  
            {  
                return RedirectToRoute(new  
                {  
                    controller = "Test",  
                    action = "Index",  
                     
                });  
            }  
        }  
    

    When you visit the Test method in TestController, he will be redirected to the Index page.

    Result:

    100044-fasfafafasfwetwet.png

    You can also view more usage of RedirectToRoute.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.