How to retrieve values from a URL in MVC

Home > Search > How-to
  by

In this example, let's say we want to display something unique based on the id value in the URL. For example, let's say we have these two URLs:

  • www.example.com/MVC/Home/Index?username=Jeremy
  • www.example.com/MVC/Home/Index?username=Natalie

When Jeremy is requested, we want to display "Welcome Jeremy". When Natalie is requested, we want to display " Welcome Natalie" Add the following to Index string of the HomeController class:

public string Index(string username)
{
    return "Welcome " + username;
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments