Bài giảng Lập trình web: Controller - Nguyễn Hà Giang
lượt xem 4
download
Nội dung chính trong chương 2 gồm: Define and describe controllers, describe how to work with action methods, explain how to invoke action methods, explain routing requests, describe URL patterns. Mời các bạn tham khảo.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Bài giảng Lập trình web: Controller - Nguyễn Hà Giang
- Controller Nguyen Ha Giang 1
- Objectives 2 • Define and describe controllers • Describe how to work with action methods • Explain how to invoke action methods • Explain routing requests • Describe URL patterns
- Working with Controllers 3 • A controller, in an ASP.NET app does the following – Manages the flow of the app. – Is responsible for intercepting incoming requests and executing the appropriate app code. – Communicate with the models of the app and selects the required view to be rendered for the request. – Is a C# class that extends the Controller class of the System.Web.Mvc namespace.
- Working with Controllers 4 • A controller is responsible to: – Locate the appropriate method to call for an incoming request. – Validate the data of the incoming request before invoking the requested method. – Retrieve the request data and passing it to requested method as arguments. – Handle any exceptions that the requested method throws. – Help in rendering the view based on the result of the requested method.
- Creating a Controller 5 • In ASP.NET MVC, the ControllerBase class of the System.Web.Mvc namespace is the base class for all controllers. • The Controllers class extends the ControllerBase class to provide a default implementation of a controller. • To create a controller in an ASP.NET MVC app, you will need to create a C# class that extends the Controller class. • Instead of creating a controller manually,
- Creating a Controller 6 • In VS 2013 IDE, you can create a controller by performing the following steps: – Right-click the Controllers folder in the Solution Explorer window. – Select Add Controller from the context menu that appears. The Add Scaffold dialog box is displayed.
- Creating a Controller 7 • Select the Empty MVC Controller in scaffolding options. • Type TestController in the controller name • Click Add. The solution Explorer window displays the newly created TestController controller under the Controllers folder.
- Creating a Controller 8 • Following figure shows the Solution Explorer window that displays the newly created controller under the Controllers folder:
- Creating a Controller 9 • Following is the skeleton code of a Controller class: using System.Web.Mvc; namespace MVCDemo.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } } }
- Working with Action methods 10 • A controller class can contains one or more action methods, also known as controller actions. • Action methods: – Are responsible for processing the requests that are sent to the controller. – Typically returns an ActionResult object that encapsulates the result of executing the method. • Following figure shows the working of action methods
- Working with Action methods 11 MVC Framework 1 HTTP Request URL http://mvcexample.com/Home/Index HomeController 2 Invoke Web Index() Browser 3 Action Method ActionResult 4 HTTP Response
- Working with Action methods 12 • The steps in the preceding figure are as follows: – The browser sends an HTTP request. – The MVC Framework invokes the controller action method based on the request URL. – The action method executes and returns an ActionResult object. This object encapsulates the result of the action method execution. – The MVC Framework convert an ActionResult to HTTP response and sends the response back to the browser.
- Working with Action Methods 13 • Rule that you need to consider while creating an action method are as follows: – They must be declared as public – They cannot be declared as static – They cannot have overloaded versions based on parameters • Following is the syntax for creating an action method in a Controller class: public ActionResult () { /*Code to execute logic and return as ActionResult */ }
- Working with Action Methods 14 • Following code creates two action methods with the name Index and About in the HomeController controller class: • The code creates 2 action methods, named Index and About in the HomeController controller class. Both these action methods are declared as public and to return ActionResult objects using System.Web.Mvc; public class TestController : Controller { public ActionResult Index() { // TODO HERE return View(); } public ActionResult About() { // TODO HERE return View();
- Working with Action Methods 15 • Although, most of the action methods return an ActionResult object, an action method can also return other types, such as String, int, or bool, as shown in the following code: using System.Web.Mvc; public class TestController : Controller { public string Index() { return "Hi World"; } public int About() { return 123; } }
- Action Results 16 • ActionResult: – Is an abstract base class for all implementing classes that provides different types of results. – Consits of HTML in combination with server- side and client-side scripts to respond to user actions. • Following table shows the commonly used classes that extend the ActionResult class to provide different implementations of the results of an method:
- Action Results 17 Classes Description ViewResult Render a view as an HTML document PartialViewResult Render a partial view, which is a sub-view of main view EmptyResult Returns an empty response RedirectResult Redirect a response to another action method JsonResult Return the result as JSON JavaScriptResult Returns JS that executes on the client browser ContentResult Returns the content based on a defined content type, such as XML FileContentResult Returns the content of a binary file FileStreamResult Returns the content of a file using a Stream object FilePathResult Returns a file as a response
- Invoking Action Methods 18 • In an ASP.NET MVC app, you can create multiple action methods in a controller. • You can invoke an action method by specifying a URL in the Web browser containing the name of the controller and the action method to invoke. http://// • Where, – : is the domain name of the app
- Invoking Action Methods 19 • Consider the following URL: http://mvcexample.com/home/Registration • When this URL is sent to the app through a Web browser, the MVC framework perform the following tasks: – Searches for the HomeController controller class – Searches for the Registration() action method in the HomeController controller class
- Invoking Action Methods 20 Application hosted on http//mvcexample.com/Home/Registration HTTP Request URL http://mvcexample.com/Home/Registration HomeController 1 Web Registration() 2 Browser Action Method 3 4 HTTP Response
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Bài giảng Lập trình Window: Chương 7 - Phan Trọng Tiến
64 p | 201 | 37
-
Bài giảng Lập trình WebForm: Web server control - ThS. Nguyễn Hà Giang
50 p | 144 | 23
-
Bài giảng Lập trình Web ASP.NET: Chương 5 - ĐH Lạc Hồng
33 p | 110 | 21
-
Bài giảng Lập trình Web ASP.NET: Chương 3 - ĐH Lạc Hồng
63 p | 93 | 18
-
Bài giảng Lập trình web với ASP.Net
386 p | 103 | 18
-
Bài giảng Lập trình Net với C# - Chương 6: Lập trình WebForm với C#
32 p | 184 | 16
-
Bài giảng Lập trình Web ASP.Net: Chương 6 - Dương Thành Phết
25 p | 118 | 12
-
Bài giảng Lập trình WebForm: Xây dựng ứng dụng Web Form – ASP.NET - ThS. Nguyễn Hà Giang
35 p | 115 | 10
-
Bài giảng Lập trình Web: Bài 4 - Trần Quang Diệu
43 p | 85 | 10
-
Bài giảng Lập trình website ASP.NET: Phần 1 - Trường ĐH Công nghiệp Quảng Ninh
78 p | 19 | 8
-
Bài giảng Lập trình WebForm: Validation control - ThS. Nguyễn Hà Giang
39 p | 94 | 6
-
Bài giảng Lập trình ứng dụng web - Nguyễn Thị Thanh Thuận
105 p | 19 | 6
-
Bài giảng Lập trình ứng dụng Web - Chương 3: HTML Server control và Web Server Control
66 p | 80 | 6
-
Bài giảng Lập trình ứng dụng Web - Chương 9: Site navigation và User control
30 p | 58 | 5
-
Bài giảng Lập trình ứng dụng Web - Chương 7: Kết gán dữ liệu
29 p | 55 | 4
-
Bài giảng môn Lập trình Web: Phần 3.1 - TS. Trần Quang Diệu
43 p | 22 | 3
-
Bài giảng môn Lập trình Web: Phần 3.2 - TS. Trần Quang Diệu
22 p | 28 | 3
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn