Danh mục tài liệu

Bài giảng Lập trình web: Controller - Nguyễn Hà Giang

Số trang: 31      Loại file: pptx      Dung lượng: 189.67 KB      Lượt xem: 11      Lượt tải: 0    
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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.
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ài liệu được xem nhiều:

Tài liệu có liên quan: