ASP.NET MVC Controller Actions and Parameters: Handling User Requests
Understand how to define and utilize controller actions and parameters in ASP.NET MVC. This tutorial explains routing, action method return types (`ActionResult`, `ViewResult`, etc.), handling different request types, and passing parameters to controller actions for building dynamic and responsive web applications.
ASP.NET MVC Controller Actions and Parameters
Controller Actions and User Requests
In ASP.NET MVC, controllers handle user requests and return responses (typically views). A controller contains multiple action methods, each designed to handle a specific type of request (e.g., displaying a page, processing form data). User requests can come from various sources (typing a URL, clicking a link, submitting a form). The MVC framework uses routing rules (defined in Global.asax.cs
) to map the incoming URL to the appropriate controller and action.
ActionResult Return Types
Action methods in ASP.NET MVC return an instance of the ActionResult
class or a derived type. The specific type depends on what the action needs to do:
Action Result Type | Helper Method | Description |
---|---|---|
ViewResult |
View() |
Renders a view (Web page). |
PartialViewResult |
PartialView() |
Renders a partial view (a reusable portion of a view). |
RedirectResult |
Redirect() |
Redirects to a URL. |
RedirectToRouteResult |
RedirectToAction() , RedirectToRoute() |
Redirects to another action method. |
ContentResult |
Content() |
Returns custom content (e.g., text, JSON). |
JsonResult |
Json() |
Returns a JSON object. |
JavaScriptResult |
JavaScript() |
Returns JavaScript code to be executed on the client. |
FileResult |
File() |
Returns a file. |
EmptyResult |
(None) | Returns an empty result. |
Adding an Action Method
To add a new action method to a controller, you define a public method within the controller class. Here's an example of adding a "Welcome" action to the MusicStoreController:
C# Controller Code (MusicStoreController.cs)
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
public class MusicStoreController : Controller
{
public ActionResult Index() { return View(); }
public string Welcome() { return "Hello, this is a welcome message."; }
}
}
Output (Accessing Welcome Action)
(A screenshot showing the output "Hello, this is a welcome message." after navigating to the URL MusicStore/Welcome
would be included here.)
Action Method Parameters
Action methods can accept parameters to receive data from the user's request (URL parameters, form data, etc.). The MVC framework maps values from the request to the action method's parameters.
C# Controller Code with Parameter (MusicStoreController.cs)
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
public class MusicStoreController : Controller
{
public ActionResult Index() { return View(); }
public string ShowMusic(string MusicTitle) { return "You selected " + MusicTitle + " Music"; }
}
}
Output (Accessing ShowMusic Action with Parameter)
(A screenshot showing the output "You selected Classic Music" after navigating to the URL MusicStore/ShowMusic?MusicTitle=Classic
would be included here.)