The document discusses ASP.NET MVC controllers, which respond to user input and collaborate between the model, view, and data access layers. Controllers contain action methods that process requests. Actions return ActionResults like ViewResult to tell the framework what to do next, without specifying how. Model binding populates action parameters from request values. Views are rendered to display HTML, located based on the controller and action name.
The document discusses ASP.NET MVC controllers, which respond to user input and collaborate between the model, view, and data access layers. Controllers contain action methods that process requests. Actions return ActionResults like ViewResult to tell the framework what to do next, without specifying how. Model binding populates action parameters from request values. Views are rendered to display HTML, located based on the controller and action name.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
ENTERPRISE APPLICATION
DEVELOPMENT
ASP.NET MVC
Resource Person: Noor ullah khan
Email: [email protected] CONTROLLERS In the context of the MVC architectural pattern, a controller responds to user input (e.g., a user clicking a Save button) and collaborates between the model, view, and (quite often) data access layers. In an ASP.NET MVC application, controllers are classes that contain methods that are called by the routing framework to process a request.
To see an example of an ASP.NET MVC controller, take a look at the HomeController
class found in Controllers/HomeController.cs: using System.Web.Mvc; namespace Ebuy.Website.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Your app description page."; return View(); } public ActionResult About() { ViewBag.Message = "Your quintessential app description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your quintessential contact page."; return View(); } } } CONTROLLER ACTIONS For instance, the HomeController class we just looked at contains three actions: Index, About, and Contact. Thus, given the default route pattern {controller}/{action}/{id}, when a request is made to the URL /Home/About, the routing framework determines that it is the About() method of the HomeController class that should process the request. The ASP.NET MVC Framework then creates a new instance of the Home Controller class and executes its About() method. In this case, the About() method is pretty simple: it passes data to the view via the ViewBag property (more on that later), and then tells the ASP.NET MVC Framework to display the view named About by calling the View() method, which returns an ActionResult of type ViewResult. ACTION RESULTS
It is very important to note that it is the
controllers job to tell the ASP.NET MVC Framework what it should do next, but not how to do it. This communication occurs through the use of +ActionResult+s, the return values which every controller action is expected to provide. For example, when a controller decides to show a view, it tells the ASP.NET MVC Framework to show the view by returning a ViewResult. It does not render the view itself. This loose coupling is another great example of separation of concerns in action(what to do versus how it should be done). CONTROLLER ACTIONS Despite the fact that every controller action needs to return an ActionResult, you will rarely be creating them manually. Instead, youll usually rely on the helper methods that the System.Web.Mvc.Controller base class provides, such as: Content() Returns a ContentResult that renders arbitrary text, e.g., Hello, world! File() Returns a FileResult that renders the contents of a file, e.g., a PDF. HttpNotFound() Returns an HttpNotFoundResult that renders a 404 HTTP status code response. JavaScript():: Returns a JavaScriptResult that renders JavaScript, e.g., function hello() { alert(Hello, World!); }. Json() Returns a JsonResult that serializes an object and renders it in JavaScript Object Notation (JSON) format, e.g., { Message: Hello, World! }. CONTINUE PartialView() Returns a PartialViewResult that renders only the content of a view (i.e., a view without its layout). Redirect() Returns a RedirectResult that renders a 302 (temporary) status code to redirect the user to a given URL, e.g., 302 http://www.ebuy.com/auctions/recent. This method has a sibling, RedirectPermanent(), that also returns a RedirectResult, but uses HTTP status code 301 to indicate a permanent redirect rather than a temporary one. RedirectToAction() and RedirectToRoute() Act just like the Redirect() helper, only the framework dynamically determines the external URL by querying the routing engine. Like the Redirect() helper, these two helpers also have permanent redirect variants: RedirectToActionPermanent() and RedirectToRoutePermanent(). View() Returns a ViewResult that renders a view. As you can tell from this list, the framework provides an action result for just about any situation you need to support, and, if it doesnt, you are free to create your own! ACTION PARAMETERS Controller actions arewhen it comes down to itjust like any other method. In fact, a controller action can even specify parameters that ASP.NET MVC populates, using information from the request, when it executes. This functionality is called model binding, and it is one of ASP.NET MVCs most powerful and useful features. Before diving into how model binding works, first take a step back and consider an example of the traditional way of interacting with request values: public ActionResult Create() { var auction = new Auction() { Title = Request["title"], CurrentPrice = Decimal.Parse(Request["currentPrice"]), StartTime = DateTime.Parse(Request["startTime"]), EndTime = DateTime.Parse(Request["endTime"]), }; // ... } MODEL BINDING BASICS Not only does model binding avoid all of this explicit code, it is also very easy to apply. So easy, in fact, that you dont even need to think about it. For example, heres the same controller action as before, this time using model-bound method parameters:
Request explicitly, the action declares them as parameters. When the ASP.NET MVC framework executes this method, it attempts to populate the actions parameters using the same values from the request that the previous That we had discussed in earlier slides. Note that even though were not accessing the Request dictionary directlythe parameter names are still very important, because they still correspond to values from in the Request. VIEWS In the ASP.NET MVC Framework, controller actions that wish to display HTML to the user return an instance of ViewResult, a type of ActionResult that knows how to render content to the response. When it comes time to render the view, the ASP.NET MVC Framework will look for the view using the name provided by the controller. Take the Index action in the HomeController: public ActionResult Index() { ViewBag.Message = "Your app description page."; return View(); } This action takes advantage of the View() helper method to create a ViewResult. Calling View() without any parameters, as in this example, instructs ASP.NET MVC to find a view with the same name as the current controller action. In this instance,