Now we're going to explore the MVC Controller in details.
We saw that Controllers respond to browser requests, the controller might return a specific view or redirects to another controller
The following code demonstrates a Controller class for a view called Demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace HelloWorld.Controllers
{
public class DemoController : Controller
{
//
// GET: /Demo/
public ActionResult Index()
{
return View();
}
public ActionResult Page2()
{
return View();
}
}
}
This class has two functions: Index() which returns Index.aspx (located in Views/Demo/Index.aspx) when the user types
[URL]/Demo in the browser.
The second function is Page2() which returns Page2.aspx (located in Views/Demo/Page2.aspx) when the user types [URL]/Demo/Page2 in the browser
The controller exposes Controller Actions, an action is a function on a controller that is invoked when a certain url is entered in the browser.
so when the user types [URL]/Demo/Page2, Page2() function is a controller action on DemoController.cs
The Controller Action must be:
- Public: Any function declared as public in a Controller is exposed as Controller Action automatically. So be careful when declaring public methods as they are accessible by the user if the correct URL is typed.
- Controller Action can not be overloaded.
- Controller Action can not be static.
ActionResul tobject:
The ActionResult can be one of these
- ViewResult – Represents HTML and markup.
- EmptyResult – Represents no result.
- RedirectResult – Represents a redirection to a new URL.
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
All these ActionResults inherit from ActionResult base class.
In our Index() Method we saw that it returns a Viewresult by calling the View() method.Normally you don't return an ActionResult directly instead you call one of these methods of the Controller base class
- View – Returns a ViewResult action result.
- Redirect – Returns a RedirectResult action result.
- RedirectToAction – Returns a RedirectToRouteResult action result.
- RedirectToRoute – Returns a RedirectToRouteResult action result.
- Json – Returns a JsonResult action result.
- JavaScriptResult – Returns a JavaScriptResult.
- Content – Returns a ContentResult action result.
- File – Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.
Consider the following functions
public string Index()
{
return "Hello World";
}
public DateTime Page2()
{
return DateTime.Now;
}
The Index method just returns a string "Hello World"
The Page2() method retuns the current date and time as a plain text
No comments:
Post a Comment