Top Banner
ASP.NET MVC Part I Ventsislav Popov Developer Evangelist at Microsoft ventsypopov.co ventsypopov.co
36

ASP.NET MVC Part I

Jan 15, 2016

Download

Documents

Gayle Morse

ASP.NET MVC Part I. Ventsislav Popov Developer Evangelist at Microsoft. ventsypopov.com. ventsypopov.com. Agenda. Beforehand – ASP.NET Web Forms What is MVC What is ASP.NET MVC? Models Views Controllers Validation Routing Unit Tests View engines. ventsypopov.com. 2. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASP.NET MVC Part I

ASP.NET MVC Part I

Ventsislav PopovDeveloper Evangelist at Microsoft

ventsypopov.

com

ventsypopov.

com

Page 2: ASP.NET MVC Part I

AgendaBeforehand – ASP.NET Web FormsWhat is MVCWhat is ASP.NET MVC?ModelsViewsControllersValidationRoutingUnit TestsView engines

2ventsypopov.

com

Page 3: ASP.NET MVC Part I

ASP.NET Web FormsRich controls and toolsPostbacksEvent driven web developmentViewstateLess control over the HTMLHard to testRapid development

3ventsypopov.

com

Page 4: ASP.NET MVC Part I

Let’s chat for a bit…

4ventsypopov.

com

Page 5: ASP.NET MVC Part I

What is MVC

5

Page 6: ASP.NET MVC Part I

Model – View - Controller

6

Controller - responsible for handling all user input

Model - represents the logic of the application

View - the visual representation of the model

ventsypopov.

com

Page 7: ASP.NET MVC Part I

ASP.NET MVCMore control over HTMLNo Codebehind Separation of concerns Easy to testURL routingNo postbacks No ViewState

7ventsypopov.

com

Page 8: ASP.NET MVC Part I

ModelsThe model should contain all of the

application business logic, validation logic, and database access logic.

ASP.NET MVC is compatible with any data access technology (for example LINQ to SQL)

All .edmx files, .dbml files etc. are located in the Models folder.

8ventsypopov.

com

Page 9: ASP.NET MVC Part I

Custom View Models

9

When you combine properties to display on a View

namespace ContosoUniversity.ViewModels{ public class AssignedCourseData { public int CourseID { get; set; } public string Title { get; set; } public bool Assigned { get; set; } }}

ventsypopov.

com

Page 10: ASP.NET MVC Part I

Creating a Model - DEMO

10

ventsypopov.

com

Page 11: ASP.NET MVC Part I

What is Controller? It is a classDerives from the base

System.Web.Mvc.Controller classGenerates the response to the

browser request

11

public class HomeController : Controller{ public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!";

return View(); }

public ActionResult About() { return View(); }}

ventsypopov.

com

Page 12: ASP.NET MVC Part I

Controller ActionsPublic method of the Controller

classCannot be overloadedCannot be a static methodReturns action result

12

public ActionResult About(){

return View();}

ventsypopov.

com

Page 13: ASP.NET MVC Part I

Action ResultsController action response to a

browser request Inherits from the base

ActionResult classDifferent results types

13

ventsypopov.

com

Page 14: ASP.NET MVC Part I

Implement a Controller - DEMO

14

ventsypopov.

com

Page 15: ASP.NET MVC Part I

Action Results TypesViewResult EmptyResultRedirectResult JsonResult JavaScriptResultContentResult FileContentResult FileStreamResultFilePathResult

15

ventsypopov.

com

Page 16: ASP.NET MVC Part I

Controller base class methodsView

Redirect RedirectToAction RedirectToRoute Json JavaScriptResult Content File

16

ventsypopov.

com

Page 17: ASP.NET MVC Part I

ViewsMost of the Controller Actions

return viewsThe path to the view is inferred

from the name of the controller and the name of the controller action. \Views\ControllerName\

ControllerAction.aspxA view is a standard (X)HTML

document that can contain scripts.script delimiters <% and %> in the

views 17

ventsypopov.

com

Page 18: ASP.NET MVC Part I

Pass Data to a ViewWith ViewData:

ViewData["message"] = "Hello World!";

Strongly typed ViewData: ViewData.Model = OurModel;

With ViewBag:

ViewBag.Message = "Hello World!";

18

ventsypopov.

com

Page 19: ASP.NET MVC Part I

Post data to a controller Verb Attributes

The action method in the controller accepts the values posted from the view.

The view form fields must match the same names in the controller.

19

ventsypopov.

com

[HttpPost]public ActionResult Edit(Movie movie){

if (ModelState.IsValid){

db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index");

}return View(movie);

}

Page 20: ASP.NET MVC Part I

Explore a View - DEMO

20

ventsypopov.

com

Page 21: ASP.NET MVC Part I

HTML HelpersMethods which typically return

string.Used to generate standard HTML

elements textboxes, dropdown lists, links etc. Example: Html.TextBox() method

Usage is optionalYou can create your own HTML

Helpers

21

ventsypopov.

com

Page 22: ASP.NET MVC Part I

ValidationTwo types of validation error

messages generated before the HTML form

fields are bound to a class generated after the form fields are

bound to the classModel StateValidation Helpers

Html.ValidationMessage() Html.ValidationSummary()

22

ventsypopov.

com

Page 23: ASP.NET MVC Part I

Implement validation- DEMO

23

ventsypopov.

com

Page 24: ASP.NET MVC Part I

RoutingThe Routing module is responsible

for mapping incoming browser requests to particular MVC controller actions.

Two places to setup: Web.config file Global.asax file

24

ventsypopov.

com

Page 25: ASP.NET MVC Part I

Routing SetupWeb.config file

25

<system.web><httpModules>

<system.web><httpHandlers>…

<system.webServer> <modules> …

<system.webServer> <handlers> …

ventsypopov.

com

Page 26: ASP.NET MVC Part I

Routing SetupGlobal.asax file

26

public class MvcApplication : System.Web.HttpApplication{

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home",

action = "Index", id = "" } );

}

protected void Application_Start() { RegisterRoutes(RouteTable.Routes); }}

ventsypopov.

com

Page 27: ASP.NET MVC Part I

URL Example

http://www.mysite.com/Home/About/6

{controller} = Home {action} = About {id} = 6

27

ventsypopov.

com

ventsypopov.

com

Page 28: ASP.NET MVC Part I

Routing example - DEMO

28

ventsypopov.

com

Page 29: ASP.NET MVC Part I

Unit TestsUsed for the business logic (not

DAL or View logic).Test individual “unit”of codeMake the code safe to modifyMock Object framework

When you lack “real” objects Create mocks for the classes in the

application Test with mock objects

29

ventsypopov.

com

Page 30: ASP.NET MVC Part I

Unit Tests - DEMO

30

ventsypopov.

com

Page 31: ASP.NET MVC Part I

View EnginesHandles the rendering of the view

to UI (html/xml);Different view engines have

different syntaxASP.NET MVC 3 Pre-included View

Engines:Web FormsRazor

31

ventsypopov.

com

Page 32: ASP.NET MVC Part I

View Engines - DEMO

32

ventsypopov.

com

Page 33: ASP.NET MVC Part I

Things to rememberWhat MVC stands forHow ASP.NET MVC differs from

Web FormsWhere is routing configuredHow to validate business logicHow to use helpersUnit tests basicsChoice between “View Engines”

33

ventsypopov.

com

Page 34: ASP.NET MVC Part I

Useful siteshttp://www.asp.net/mvchttp://msdn.microsoft.com/en-us/library/dd394709.aspx

http://stackoverflow.com/http://jquery.com/

34

ventsypopov.

com

Page 35: ASP.NET MVC Part I

Questions?

ASP.NET MVC

ventsypopov.

com

Email: vepopov [at] microsoft.comTwitter: @v_popov

Page 36: ASP.NET MVC Part I

Time to wake up :)

36