RedirectToAction usage in asp.net mvc

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?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)