Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction usage in asp.net mvc

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 following: first, we create get & set properties for the person data:

public class Person {     public int personID {get;set;}     public string personName {get;set;}     public string nric {get;set;} } 

and after login, we put the data in a class Person object and we use RedirectToAction like this:

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

It's working normally, but the parameter are shown in the URL. How can I hide them and also can I hide the action name? Guide me the right way with some examples, please.

like image 878
Chong Avatar asked Dec 06 '10 02:12

Chong


People also ask

What is the usage of a RedirectToAction?

The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.

What is the difference between RedirectToAction () and RedirectToRoute () in MVC?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

Can we pass model to RedirectToAction in MVC?

When a Button is clicked, the Model object is populated with values and passed to the RedirectToAction method along with the name of the Controller and its Action method in ASP.Net MVC Razor. In this article I will explain with an example, how to redirect to Action method with Model data in ASP.Net MVC Razor.


1 Answers

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?

like image 194
RPM1984 Avatar answered Sep 19 '22 21:09

RPM1984