overview of code coverage & code...

26
.NET MVC a Jump Start © Meganadha Reddy K., 2015 http://www.netcomlearning.com/ Meganadha Reddy K. Technical Trainer | NetCom Learning www.NetComLearning.com

Upload: others

Post on 15-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

.NET MVCa Jump Start

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Meganadha Reddy K.Technical Trainer | NetCom Learning

www.NetComLearning.com

Page 2: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

What is ASP.NET MVC ?

• A web application project type

• Points to note here is:

• It is not replacement for web forms

• Builds on top of ASP.NET

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 3: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

What is MVC for?

• Maintain Clean Separation of Concerns

• Extensible and Pluggable

• Enable clean URLs and HTML

• Integration with ASP.NET

• Easily testable

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 4: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Model

View

Controller

Page 5: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC – How it works? (1 of 2)

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Controller

View

Model

USER

Uses Manipulates

UpdatesSees

Page 6: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC – How it works? (2 of 2)

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 7: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

M, V, C – Definitions (1 of 2)

MVC stands for model-view-controller. MVC is a pattern for developing

applications that are well architected, testable and easy to maintain.

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 8: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

M, V, C – Definitions (2 of 2)

• Models: Classes that represent the data of the application and that use

validation logic to enforce business rules for that data.

• Views: Template files that your application uses to dynamically generate

HTML responses.

• Controllers: Classes that handle incoming browser requests, retrieve model

data, and then specify view templates that return a response to the browser.

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 9: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Lets create a simple MVC project

From Visual Studio:

File “New Project”

Under “Web”

ASP.NET Web Application

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 10: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Lets understand the default folders

Points to note here:

a. Folders for

i. Models

ii. Views

iii. Controllers

b. App_Start RouteConfig.cs

c. Global.asax

d. package.config

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 11: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

RouteConfig.cs

Will have default path or

route for the website

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 12: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

First MVC Application

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 13: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Adding – Controller Class

Namespace

Controllers should inherit Controller

ActionResult is the return type here.

1

2

3

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 14: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Adding – View

Step-1:

Add a folder “Home” inside “Views”

Step-2:

Right click on “Home”.

Add View [As shown]

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 15: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Simple - Model

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 16: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Updated Controller

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 17: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

ASP.NET MVC Request

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 18: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Templates in Visual Studio

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 19: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Razor – Syntax (1 of 2)

19

@ - For values (HTML Encoded)

<p>

Current time is: !!!

</p>

@{ … } – For code blocks (keep the view simple!)

@{

}

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 20: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Razor – Syntax (2 of 2)

• If, else, for, foreach, etc. C# statements

• HTML markup lines can be included at any part

• @: – For plain text line to be rendered

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 21: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Flow (Summary)

Request

Controller

Step 1

Incoming request directed to Controller

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 22: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Flow (Summary)

ControllerModel

Step 2

Controller processes request and forms a data Model

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 23: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Flow (Summary)

Controller

ViewStep 3

Model is passed to View

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 24: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Flow (Summary)

Controller

View

Step 4

View transforms Model into appropriate output format

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 25: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

MVC Flow (Summary)

Response

Controller

View

Step 5

Response is rendered

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Page 26: Overview of Code Coverage & Code Analysisa.netcominfo.com/pdf/ASPNET_MVC_JumpStart_Webinar_NetCom.pdf · M, V, C –Definitions (2 of 2) •Models: Classes that represent the data

Thank you

?

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/