RedirectToAction and RedirectToRoute

后端 未结 1 1554
醉酒成梦
醉酒成梦 2020-12-09 02:58

In my controller of webpage 1, I want to redirect to Webpage 2, passing 2 variables.

I tried using RedirectToRoute, but cannot get it to work; wrong URL is displayed

相关标签:
1条回答
  • 2020-12-09 03:42

    Omit parameter defaults to make parameters required:

        routes.MapRoute(
        "CreateAdditionalPreviousNames", // Route name
        "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters
        new { controller = "UsersAdditionalPreviousNames", action = "Index" }
    );
    

    For route redirect, try this:

    return RedirectToRoute(new 
    { 
        controller = "UsersAdditionalPreviousNames", 
        action = "Index", 
        userId = user.Id, 
        applicantId = applicant.Id 
    });
    

    Another habit I picked up from Steve Sanderson is not naming your routes. Each route can have a null name, which makes you specify all parameters explicitly:

        routes.MapRoute(
        null, // Route name
        "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters
        new { controller = "UsersAdditionalPreviousNames", action = "Index" }
    );
    
    0 讨论(0)
提交回复
热议问题