Action parameters are defined as the variables which are used to retrieve user-requested values from the URL.
The parameters are easily retrieved from the request's data collection. Parameters may include name or value pairs for data, query string value, etc. the controller class will locate for the values of the parameters based on the RouteData instance. If the value will present, it will be passed to the parameter else exception will be thrown.
The Controller class gives two properties Request and Response which can be used to get handle user requests and responses.
Example
Here, we are going to create an action method in the controller. This action method will have a parameter. The controller code may look like this:
// MusicStoreController.cs
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.Mvc;
6. namespace MvcApplicationDemo.Controllers
7. {
8. public class MusicStoreController : Controller
9. {
10. // GET: MusicStrore
11. public ActionResult Index()
12. {
13. return View();
14. }
15. public string ShowMusic(string MusicTitle)
16. {
17. return "You selected " + MusicTitle + " Music";
18. }
19. }
20. }
Output:
In URL, we must have to pass the parameter value and we will do it by this URL localhost:port-no/MusicStore/ShowMusic?MusicTitle=Classic.
It will give the following output:-
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Action parameters are defined as the variables which are used to retrieve user-requested values from the URL.
The parameters are easily retrieved from the request's data collection. Parameters may include name or value pairs for data, query string value, etc. the controller class will locate for the values of the parameters based on the RouteData instance. If the value will present, it will be passed to the parameter else exception will be thrown.
The Controller class gives two properties Request and Response which can be used to get handle user requests and responses.
Example
Here, we are going to create an action method in the controller. This action method will have a parameter. The controller code may look like this:
// MusicStoreController.cs
Output:
In URL, we must have to pass the parameter value and we will do it by this URL localhost:port-no/MusicStore/ShowMusic?MusicTitle=Classic.
It will give the following output:-