
Bài giảng Lập trình web: Controller - Nguyễn Hà Giang
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình web: Controller - Nguyễn Hà GiangControllerNguyen 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 1HTTP 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(); ...
Tìm kiếm theo từ khóa liên quan:
Lập trình web Bài giảng Lập trình web Working with controllers Action methods Invoke action methods Routing requestsTài liệu có liên quan:
-
161 trang 139 1 0
-
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 137 0 0 -
Bài giảng Lập trình web nâng cao: Chương 8 - Trường ĐH Văn Hiến
36 trang 124 1 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 118 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 110 0 0 -
Lập Trình Web: Các trang quản trị trong PHP - GV: Trần Đình Nghĩa
8 trang 108 0 0 -
231 trang 99 1 0
-
101 trang 97 2 0
-
Bài giảng Lập trình web nâng cao: Chương 7 - Trường ĐH Văn Hiến
16 trang 69 1 0 -
Bài giảng Lập trình web bài 2: Làm quen với công cụ vẽ và sử dụng Symbol bảng Library
51 trang 58 0 0 -
Bài giảng Lập trình Web ASP.Net với C#: Chương 9 - Th.S Phạm Đào Minh Vũ
55 trang 56 0 0 -
Lập trình web bằng cơ sở dữ liệu MySQL 5.1 và PHP 5.3: Phần 1
136 trang 47 1 0 -
Tạo mạng xã hội với PHP - part 43
10 trang 46 0 0 -
Bài giảng Thiết kế và lập trình Web - Bài 5: PHP cơ bản
137 trang 44 0 0 -
Giáo trình môn học/mô đun: Lập trình web với ASP.NET: Phần 2
42 trang 43 0 0 -
Hướng dẫn thiết kế template joomla 1.5
10 trang 43 0 0 -
Bài giảng Lập trình web nâng cao: Chương 1 - Trường ĐH Văn Hiến
16 trang 41 1 0 -
Bài giảng Thiết kế và lập trình Web - Bài 8: PHP - MySQL
59 trang 40 0 0 -
Bài giảng Lập trình Web ASP.Net với C#: Chương 6 - Th.S Phạm Đào Minh Vũ
31 trang 40 0 0 -
Bài giảng Lập trình Web: Chương 2 - Ths. Trần Phi Hảo
54 trang 40 0 0