ASP.NET Controller Actions and Parameters
In 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 Type
The 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.
Action Result | Helper Method | Description |
---|---|---|
ViewResult | View | It is used to render a view as a Web page. |
PartialViewResult | PartialView | It is used to render a partial view. |
RedirectResult | Redirect | It is used to redirect to another action method by using its URL. |
RedirectToRouteResult | RedirectToAction RedirectToRoute | It is used to redirect to another action method. |
ContentResult | Content | It is used to return a user-defined content type. |
JsonResult | Json | It is used to return a serialized JSON object. |
JavaScriptResult | JavaScript | It is used to return a script that can be executed on the client. |
FileResult | File | It is used to return binary output to write to the response. |
EmptyResult | (None) | It represents a return value that is used if the action method must return a null result. |
Adding an Action method
Here, 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.cs
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 Parameters
Action 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.cs
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.