C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET Controller Actions and ParametersIn ASP.NET MVC application, the controller defines action methods that are used to handle user requests and render view as the response. A controller can have any number of actions. A user request can be any of like: entering URL into the browser, clicking a link or submitting a form. The MVC application uses the routing rules that are defined in the Global.asax.cs file. This file is used to parse the URL and determine the path of the controller. Now, controller executes the appropriate action to handle the user request. ActionResult Return TypeThe ActionResult class is the base class for all action results. Action methods return an instance of this class. There can be different action result types depending on the task that the action is implementing. For example, if an action is to call the View method, the View method returns an instance of the ViewResult which is derived from the ActionResult class. We can also create action method that returns any type of object like: integer, string etc. The following table contains built-in action result types.
Adding an Action methodHere, we will add a new action method to the controller that we crested in previous chapter. To add action to the existing controller, we need to define a public method to our controller. Our MusicStoreController.cs file is looks like the following after adding a welcome action method. // MusicStoreController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class MusicStoreController : Controller { // GET: MusicStrore public ActionResult Index() { return View(); } public string Welcome() { return "Hello, this is welcome action message"; } } } Output: To access the welcome action method, execute the application then access it by using MusicStore/Welcome URL. It will produce the following output. Action Method ParametersAction parameters are the variables that are used to retrieve user requested values from the URL. The parameters are retrieved from the request's data collection. It includes name/value pairs for form data, query string value etc. the controller class locates for the parameters values based on the RouteData instance. If the value is present, it is passed to the parameter. Otherwise, exception is thrown. The Controller class provides two properties Request and Response that can be used to get handle user request and response. Example Here, we are creating an action method in the controller. This action method has a parameter. The controller code looks like this: // MusicStoreController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class MusicStoreController : Controller { // GET: MusicStrore public ActionResult Index() { return View(); } public string ShowMusic(string MusicTitle) { return "You selected " + MusicTitle + " Music"; } } } Output: In URL, we have to pass the parameter value. So, we are doing it by this URL localhost:port-no/MusicStore/ShowMusic?MusicTitle=Classic. it produces the following result.
Next TopicASP.NET MVC Action Selectors
|