You are on page 1of 9

HE W A D UNIVERSITY

Asp.net
Abdul Majid Niazai
niazai216@gmail.com
ASP.NET Controller Actions

 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.

It is used to redirect to another action


RedirectResult Redirect
method by using its URL.

It is used to redirect to another action


RedirectToRouteResult RedirectToAction RedirectToRoute
method.

It is used to return a user-defined content


ContentResult Content
type.

It is used to return a serialized JSON


JsonResult Json
object.

It is used to return a script that can be


JavaScriptResult JavaScript
executed on the client.

It is used to return binary output to write


FileResult File
to the response.

It represents a return value that is used if


EmptyResult (None) 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
namespace MvcApplicationDemo.Controllers
{
public class MusicStoreController : Controller
{
// GET: MusicStrore
public ActionResult Index()
{
return View();
}
public string Welcome()
{
return "Hello, this is welcome action message";
}
}
}
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
namespace MvcApplicationDemo.Controllers
{
public class MusicStoreController : Controller
{
// GET: MusicStrore
public ActionResult Index()
{
return View();
}
public string ShowMusic(string MusicTitle)
{
return "You selected " + MusicTitle + " Music";
}
}
}
End of Chapter 12

You might also like