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.
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.
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 .
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With