RedirectToAction usage in asp.net mvc

后端 未结 4 430
甜味超标
甜味超标 2020-12-28 13:34

I want to post some questions about ASP.Net MVC. I am not familiar with web developing, But I was assigned to the web part of a project. We are doing the follow

4条回答
  •  旧巷少年郎
    2020-12-28 14:06

    The parameter are shown in the URL because that is what the third parameter to RedirectToAction is - the route values.

    The default route is {controller}/{action}/{id}

    So this code:

    return RedirectToAction("profile","person",new { personID = Person.personID});
    

    Will produce the following URL/route:

    /Person/Profile/123

    If you want a cleaner route, like this (for example):

    /people/123

    Create a new route:

    routes.MapRoute("PersonCleanRoute",
                    "people/{id}",
                    new {controller = "Person", action = "Profile"});
    

    And your URL should be clean, like the above.

    Alternatively, you may not like to use ID at all, you can use some other unique identifier - like a nickname.

    So the URL could be like this:

    people/rpm1984

    To do that, just change your route:

    routes.MapRoute("PersonCleanRoute",
                        "people/{nickname}",
                        new {controller = "Person", action = "Profile"});
    

    And your action method:

    public ActionResult Profile(string nickname)
    {
    
    }
    

    And your RedirectToAction code:

    return RedirectToAction("profile","person",new { nickname = Person.nickname});
    

    Is that what your after?

提交回复
热议问题