Controller Overview
The ASP.NET MVC framework maps URLs to classes that are referred to as controllers.
Controllers
- Process incoming requests
- Handle user input and interactions and
- Execute appropriate application
The Controller class inherits from ControllerBase and is the default implementation of a controller. The Controller class is responsible for the following processing stages:
- Locating the appropriate action method to call and validate that it can be
- Getting the values to be used as the action method’s
- Handling all errors that might occur during the execution of the action
- Providing the View for rendering ASP.NET pages to
ActionMethods and ActionResult object
ASP.NET MVC application is organized around controllers and action methods. The controller defines action
methods. Controllers can include as many action methods as needed.
- Action methods typically have a one-to-one mapping with user interactions. Examples of user interactions include entering a URL into the browser, clicking a link, and submitting a Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.
- Most action methods return an instance of a class that derives from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing. For example, the most common action is to call the View The View method returns an instance of the ViewResult class, which is derived from ActionResult.
- We can create action methods that return an object of any type, such as a string, an integer or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response
1 2 3 4 5 6 7 |
o In Controller public string SayHello(string name) { return "Hello " + name; } o In View @Html.ActionLink("Say Hello", "SayHello", new {name="Sandeep"}) |
The following table shows the built-in action result types and the action helper methods that return them:
ActionResult Inherited Classes | Action Method | Description |
ViewResult | View |
Renders a view as a Web page. |
PartialViewResult | PartialView | Renders a partial view, which defines a section of a
view that can be rendered inside another view. |
RedirectResult | Redirect
RedirectPermanent |
Redirects to another action method by using its URL. |
RedirectToRouteResult | RedirectToAction
RedirectToRoute |
Redirects to another action method. |
ContentResult | Content | Returns a user-defined content type. |
FileContentResult
FilePathResult FileStreamResult |
File | Returns binary output to write to the response. |
JavascriptResult | JavaScript | Return JavaScript content |
JsonResult | Json | Returns a serialized JSON object. |
EmptyResult | null | Returns Empty Page. |
HttpNotFoundResult | HttpNotFound | Returns 404 error. |
HttpStatusCodeResult | — | Returns the specified Status code and Description |
HttpUnauthorizedResult | — |
Return Unauthorized Http response |
Example of ViewResult
By default, the Controller actions will return the ActionResult object. We can return various types of results as ActionResult, which will decide how the output needs to render on the browser.
Example of ViewResult
1 2 3 4 |
public ActionResult About() { return View(); } |
Example of ContentResult.
1 2 3 4 5 6 7 8 9 10 11 |
public class SampleController : Controller { public ActionResult Index() { return Content("Hello from Index action in Sample Controller"); } public ActionResult RenderXML() { return Content("<Demo>This is Test</Demo>","text/xml",System.Text.Encoding.Unicode); } } |
Example of Redirect
1 2 3 4 |
public ActionResult SayHello(string name) { return Redirect("~/Home/RenderXml"); } |
Example of RedirectToAction: Depending on the input values, we can redirect to another Action.
1 2 3 4 |
public ActionResult Index() { // Following Redirect‘s to Verify action inside the Sample Controller return RedirectToAction("Verify", "Sample"); } |
Example of RedirectToRoute
When we need to redirect to a route defined in Global.asax, we will use the RedirectToRoute object. In Global.asax:
routes.MapRoute(“RenderXml”, // Route name
“Home/RenderXml”);//, // URL with parameters
1 2 3 4 |
public ActionResult Index() { return RedirectToRoute("RenderXml"); } |
Example of File
File is used to return the content of a file to the browser.
1 2 3 4 5 |
public ActionResult Index() { return File(Server.MapPath("~/Demo.xml"), "text/xml"); } Note: FileResult loads the file and renders the content to the browser without actually redirecting to the URL of mentioned file. |
Example of JavaScript:
We are render javascript library functions (like a .js file)
1 2 3 4 5 6 |
public ActionResult SayHello() { return JavaScript("alert('Hello');"); } and include the following in a View <script src="/Home/SayHello"></script> |
Example of JSON
We can render the text to the result page or can send it as a file to the client using JSON notation.
1 2 3 4 5 |
public ActionResult Index() { Person p = new Person(); p.FirstName = fname; p.LastName = lname; return Json(p, JsonRequestBehavior.AllowGet); } |
Non-Action Methods:
By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.
Example:
1 2 3 |
[NonAction] public void DoSomething() { // Method logic.} |