Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.

Similar presentations


Presentation on theme: "1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page."— Presentation transcript:

1 1 Using MVC 6

2 MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page life cycle. Web controls provide much prewritten code In MVC, browser requests a method in a class. The method produces a response HTML, JSON, other. 2

3 Getting Started You need Visual Studio 2015 ASP.NET 5 RC1 Microsoft tutorial https://public.readthedocs.com/aspnet-aspnet/en/latest/tutorials/first-mvc-app/ Open Visual Studio 2015 3

4 Create a new MVC 6 Application For an MVC application you must use New > Project Not New > Web Site 4

5 5 If you don't see this, you need to install ASP.NET RC 1 http://www.csee.usf.edu/~turnerr/Web_Application_Design/ 302_Install_ASP_NET_5_RC1.pdf Click OK

6 6

7 7

8 8 The default template gives you Home, Contact, About, Register, and Log in pages. Links in the black Nav bar at the top, if the window is wide enough. Nav bar

9 Same page in more narrow window 9 Nav bar Click here to expand Nav bar

10 Same Page with Nav Bar Expanded 10

11 MVC https://public.readthedocs.com/aspnet-aspnet/en/latest/tutorials/first- mvc-app/adding-controller.html#adding-a-controller https://public.readthedocs.com/aspnet-aspnet/en/latest/tutorials/first- mvc-app/adding-controller.html#adding-a-controller The MVC Pattern Model Data, "Business Rules", Program Logic View Presentation only Displays model data No program logic Controller Handles browser requests Invokes model functions 11

12 MVC The pattern encourages: Separation of concerns Loosely coupled components 12

13 In Visual Studio 13

14 Adding a Controller In Solution Explorer, right click on Controllers and select Add > New Item 14

15 Adding a Controller 15 Click Add

16 Controller Boilerplate 16 All controller class names end with "controller"

17 Replace the Boilerplate using Microsoft.AspNet.Mvc; using Microsoft.Extensions.WebEncoders; namespace MvcMovie.Controllers { public class HelloWorldController : Controller { // // GET: /HelloWorld/ public string Index() { return "This is my default action..."; } // // GET: /HelloWorld/Welcome/ public string Welcome() { return "This is the Welcome action method..."; } 17

18 Things to Notice 18 Default method invoked by appending /HelloWorld/ to the URL in a Get request Method invoked by appending /HelloWorld/Welcome/ to the URL in a Get request Every public method in a controller is callable from the browser. Let's try it!

19 Initial Page 19 Add /HelloWorld/ to the URL in the address box and press Enter.

20 Add /HelloWorld/ to the URL 20 Specifies the controller class

21 Output of the HelloWorldController 21

22 How it works MVC invokes controller classes (and the action methods within them) depending on the incoming URL. The default URL routing logic used by MVC uses a format like this to determine what code to invoke: /[Controller]/[ActionName]/[Parameters] The first URL segment detrmines to controller class to run. The second segment determines the action method to invoke within that class. We will discuss the thrid segment later. 22

23 Startup.cs You set the format for routing in the Startup.cs file. 23

24 Startup.cs 24 If you don't supply any URL segments, MVC uses default values: Home for controller Index for action

25 HelloWorldController Browse to http://localhost:xxxx/HelloWorld/Welcome/ The Welcome method in HelloWorldController.cs runs 25

26 Output of Welcome method in HelloWorldController 26

27 Passing Information to the Controller Let’s modify the example slightly so that you can pass some parameter information from the URL to the controller Example: /HelloWorld/Welcome?name=Scott&numtimes=4. 27

28 Passing Information to the Controller Change the Welcome method to include two parameters as shown below: public string Welcome(string name, int numTimes = 1) { return HtmlEncoder.Default.HtmlEncode( "Hello " + name + ", NumTimes is: " + numTimes); } Note that the code uses the C# optional-parameter feature to indicate that the numTimes parameter defaults to 1 if no value is passed for that parameter. The code above uses HtmlEncoder.Default.HtmlEncode to protect the app from malicious input (namely JavaScript). 28

29 Try it! 29

30 Controller Output 30 The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model BindingModel Binding

31 Another Way to Pass Data to the Controller Replace the Welcome method with the following code: public string Welcome(string name, int ID = 1) { return HtmlEncoder.Default.HtmlEncode( "Hello " + name + ", ID: " + ID); } 31

32 Another Way to Pass Data to the Controller 32 A third URL segment is specified this time. And only one query string.

33 Controller Output 33 This time the third URL segment matched the route parameter id. The Welcome method contains a parameter "id" that matched the URL template in the MapRoute method.

34 Recall the Route Setup The trailing ? (in id?) indicates that the id parameter is optional. 34

35 Summary In these examples the controller has been doing the “VC” portion of MVC Both the view and the controller work. The controller is returning HTML directly. Next we will look at using a Razor view template to help generate the HTML Response. 35


Download ppt "1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page."

Similar presentations


Ads by Google