MVC Presentation

of 20 /20

Embed Size (px)


ASP.NET MVC Introduction

Transcript of MVC Presentation


ASP.NET MVCIntroductionThe Model-View-Controller (MVC) is a software architecture pattern where we separate the architecture into three areas. This separation gives you more control over the individual parts of the application, which lets you more easily develop, modify, and test them.ASP.NET MVC is part of the ASP.NET framework.ASP.NET MVC is an option , not replacement.ASP.NET > MVCASP.NET > Web Forms

ASP.NET MVC Framework The ASP.NET framework is one part of the .NET framework.

The ASP.NET framework is Microsofts framework for building web applications. It contains a set of classes that were created specifically to support building web applications.

ASP.NET framework includes classes for implementing web page caching, authentication, and authorization.

Microsoft has two frameworks for building web applications built on top of the ASP.NET framework: ASP.NET Web Forms and ASP.NET MVC.

Display logic coupled with code, through code-behind filesHarder to unit test application logic, because of the coupled code-behind filesViewState and PostBack modelState management of controls leads to very large and often unnecessary page sizes

Drawbacks of ASP.NETAdvantages of ASP.NET MVC Provides complete control over your HTML markup. Clear separation of concerns Enables Test Driven Development (TDD). Easy integration with JavaScript frameworks like jQuery or Yahoo UI frameworks Enables rich AJAX integration No ViewState and PostBack model Supports all the core ASP.NET features, such as authentication, caching, membership, etc.5ASP.NET Versions. ASP.NET MVC 1.0 (Dec,2008) ASP.NET MVC 2.0 Beta (Nov,2009) ASP.NET MVC 3.0(Present)Requirements .NET Framework 3.5 Visual Studio 2008(service pack 1)Visual Studio 2010(included)

MVC Design PatternModelControllerViewModel=The data structureController=Does the processingView=What is presented to the clientMVC :MODEL-VIEW-CONTROLLERBrowserControllerModelViewHTTP ResponseGUI ContentHTTP RequestExecution ParametersResulting Data ArraysResulting Data ArraysASP.NET MVC Default Project Structure

This diagram represents the ASP.NET MVC default project structure. Default project has Global.asax file which includes the routing information for default application. The following are the default folders that will come when new MVC project is created./ControllersWhere you put Controller classes that handle URL requests

/ModelsWhere you put classes that represent and manipulate data

/ViewsWhere you put UI template files that are responsible for rendering output

/ScriptsWhere you put JavaScript library files and scripts (.js)

/ContentWhere you put CSS and image files, and other non-dynamic/non-JavaScript content

/App_DataWhere you store data files you want to read/write. Directory PurposeWhen we run an ASP.NET MVC application, Visual Studio launches the application in your web browser.Below is the home page for our new project (URL: "/)

MVC RoutingGenerally routing information is given in Global.asax file.

namespace MvcApplication1{ public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } //Parameter defaults ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } }}The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.

Imagine that you enter the following URL into your web browser's address bar:


The Default route maps this URL to the following parameters:

controller = Homeaction = Indexid = 3


ControllerAction method in controllerSchemaHosthttp://localhost:49450/Account/SignInControllerMVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller. A set of action methods that share a common Controller context. An Action: Receives the request parameters. Creates any required model (user, etc..). Calls services or repositories to execute data or get the models to be displayed. Selects the view and sends the model to it.Example:http://localhost:49450/Home/Index

namespace MvcApplication1.Controllers{public class HomeController : Controller { public ActionResult Index() { ViewData["Title"] = "Home Page"; ViewData["Message"] = "Welcome to ASP.NET MVC!; return View(); }

public ActionResult About() { ViewData["Title"] = "About Page; return View(); } }}View Just renderer

No logic should go there

Code Behind exists but not recommended

No ViewState, No Server Controls

Takes the view data from the controller.

Can be typed.(strongly typed i.e. model is included)

View ExampleIndex.aspx.