1 / 16

ASP.NET MVC

ASP.NET MVC. Introduction to ASP.NET MVC and Example Walkthrough Rajat Arya ( rajata@U ) eFECS - OIM DAWG – 4/21/2009. Agenda. Definition what is ASP.NET MVC? Functional Description how does ASP.NET MVC actually work? Goal / Benefits why is it important?

thuy
Download Presentation

ASP.NET MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET MVC Introduction to ASP.NET MVC and Example Walkthrough Rajat Arya (rajata@U) eFECS - OIM DAWG – 4/21/2009

  2. Agenda • Definition • what is ASP.NET MVC? • Functional Description • how does ASP.NET MVC actually work? • Goal / Benefits • why is it important? • ASP.NET MVC Implementation Walkthrough • lets see this thing actually working • Questions? • Reference Materials 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  3. ASP.NET MVC Definition The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace. (taken from http://www.asp.net/learn/mvc/tutorial-01-cs.aspx) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  4. Functional Description (is a) • Set of classes, containing: • Base class for Controllers • Base class for Views (extend ASP.NET View class) • Base class for Responses (ActionResult) • Default classes for Routing (wiring URLs to Controllers) • Plugin for Visual Studio • Adds project template 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  5. Functional Description (how it works) Registers itself with ASP.NET in Default.aspx.cs as an HttpHandler and then handles all HTTP requests public partial class _Default : Page { public void Page_Load(object sender, System.EventArgs e) { HttpContext.Current.RewritePath(Request.ApplicationPath, false); IHttpHandlerhttpHandler = new MvcHttpHandler(); httpHandler.ProcessRequest(HttpContext.Current); } } Enables Routing System through web.config changes 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  6. Functional Description (the parts) • Views • Display the application's user interface (UI). • Typically, this UI is created from the model data • Ex. an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Products object. • Controllers • Handle user interaction, work with the model, and ultimately select a view to render that displays UI. • Handles and responds to user input and interaction • Ex. handles query-string values, and passes these values to the model, which in turn queries the database by using the values. • Routing • Define rules to route URLs to specific Controllers. • Providing inputs to specific Actions defined by the Controller. • Ex. http://www.example.com/products/info/1 could be routed to ActionResultProductsController.Information(int id) method (parts taken from http://www.asp.net/learn/mvc/tutorial-01-cs.aspx) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  7. Functional Description (Views) A view is a standard (X)HTML document that can contain scripts. Can be defined as an .aspx page, but no codebehind Example: \Views\Home\Index.aspx <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Index</title> </head> <body> <div> The current date and time is <% Response.Write(DateTime.Now);%> </div> </body> </html> 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  8. Functional Description (Controllers) Class that extends System.Web.Mvc.Controller class Defines public methods that return ActionResult for Actions the controller handles Each ActionResult method accepts inputs, does the processing (works with Model / Repository / Business Logic Layer) and returns ActionResult to user A View is returned as a ViewResult from the Controller Example: ProductsController public class ProductsController : Controller { // GET: /Products/ public ActionResult Index() { // Add action logic here return View(); } } 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  9. Functional Description (Routing) Responsible for mapping browser requests to a particular MVC Controller action. Enabled in web.config file Route table created in Global.asax file, called in Application_Start() method public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "Default", // Unique route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // defaults ); routes.MapRoute( "ProductInfo", "Products/Info/{id}", new { controller = "Products", action = "Info", id = "" } // defaults ); } 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  10. Functional Description (Passing Data) • ViewData object is used to pass data between Controllers and Views • ViewData is a dictionary object, which has a Model property on it of type object • Use dictionary to pass data back, or use model object to set a model that the View is to display • Controller does the following: • return View(“ViewName”, model); • ViewData[“id”] = 23;return View(); 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  11. Functional Definition (request to response) URL request for /products/info/1 Default.aspx.cs invokes MvcHttpHandler to handle request MvcHttpHandler refers to Routing Rules registered in Global.asax.cs (in Application_Start) to find a Controller/Action that matches request (/products/info/{id}) MvcHttpHandler instantiates controller and invokes corresponding action method(using built in object mapper to map parameters on URL as inputs to action method) Calls ProductsController.Info(id=1) code Info method works with Business Logic objects to perform operation, sets any ViewData and returns a View as an ActionResult 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  12. Goals / Benefits (1) • Separation of Concerns • Separation of application tasks (input logic, business logic, and UI logic) • Does not use view state or server-based forms. • Testability • All core contracts in the MVC framework are interface-based and can be tested by using mock objects, which are simulated objects that imitate the behavior of actual objects in the application • You can unit-test the application without having to run the controllers in an ASP.NET process, which makes unit testing fast and flexible. You can use any unit-testing framework that is compatible with the .NET Framework. • Extensibility • All core contracts in the MVC framework are interface-based • Supports the use of Dependency Injection (DI) and Inversion of Control (IOC) container models. • Plug in your own view engine, URL routing policy, action-method parameter serialization, and other components. • Compatibility • ASP.NET MVC lets you use features such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture. • You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on. (parts taken from http://www.asp.net/learn/mvc/tutorial-01-cs.aspx) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  13. Goals / Benefits (2) • Powerful URL Mapping Component • Uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure. For more information, see Front Controller on the MSDN Web site. • URLs do not have to include file-name extensions, and are designed to support URL naming patterns that work well for search engine optimization (SEO) and representational state transfer (REST) addressing. • Well Documented and Lots of Active Development • Fully Supported by Microsoft • Open-Source under MS-PL license • Free! (parts taken from http://www.asp.net/learn/mvc/tutorial-01-cs.aspx) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  14. ASP.NET MVC Implementation Walkthrough Small sample ASP.NET MVC application. Taken from asp.net/mvc website (http://static.asp.net/asp.net/images/mvc/32/CS/ContactManager_7_CS.zip) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

  15. Fin. Questions?

  16. Reference Materials http://www.asp.net/learn/mvc/ - really well done – go here first! http://weblogs.asp.net/scottgu/archive/tags/MVC/ASP.NET/default.aspx - Scott Guthrie’s posts, goodness, code, http://www.hanselman.com/blog/CategoryView.aspx?category=ASP.NET+MVC – Scott Hanselman’s posts, good, less code Unity/IoC Running on IIS6 (or here) 4/21/2009 - Rajat Arya (rajata@u) - DAWG

More Related