Lab 04
Lab 04
M55340A | Module 4
M55340A | Module 4
Home
Lab Guide
Lab Guide
You have been asked to add controllers to a new application. The controllers should
include actions that return a view, also add an action that returns the photo as a .jpg file
to show on a webpage, and an action that redirects to another action in another
controller. Additionally, you have been asked to configure routes in a variety of ways.
The members of your development team are new to ASP.NET Core MVC and they find
the use of controller actions confusing. Therefore, you need to help them by adding a
component that displays action parameters in an external file whenever an action runs.
To achieve this, you will add an action filter.
Exercise 1: Adding Controllers and Actions to an MVC Application
Scenario
In this exercise, you will create the MVC controller that handles user operations. You will
also add the following actions:
Note: If a Security Warning for WorldJourney dialog box appears, verify that
the Ask me for every project in this solution check box is cleared, and then
click OK.
2. In Solution Explorer, right-click WorldJourney, point to Add, and then
select New Folder.
3. In the NewFolder box, type Controllers, and then press Enter.
4. In the WorldJourney - Microsoft Visual Studio window, in Solution Explorer,
right-click the Controllers folder, point to Add, and then select Controller....
5. In the Add New Scaffolded Item dialog box, click MVC Controller - Empty,
and then click Add.
6. In the Add New Item dialog box, in the Name: textbox, type HomeController,
and then click Add (if this is the first controller, it will already be named for you).
7. In the WorldJourney - Microsoft Visual Studio window, in Solution Explorer,
right-click the Controllers folder, point to Add, and then select Controller....
8. In the Add New Scaffolded Item dialog box, click MVC Controller - Empty,
and then click Add.
9. In the Add New Item dialog box, in the Name: textbox, type CityController, and
then click Add.
1. In the CityController class code block, in the Index action code block, locate
the following code:
2. return View();
3. Place the cursor before the located code, and type the following code:
4. ViewData["Page"] = "Search city";
5. In the CityController code window, ensure that the cursor is at the end of
the Index action code block, press Enter, and then type the following code:
6. public IActionResult Details()
7. {
8. }
9. In the Details action code block, type the following code:
10. ViewData["Page"] = "Selected city";
11. City city = null;
12. if (city == null)
13. {
14. return NotFound();
15. }
16.
17. return View(city);
18. Verify that Visual Studio has automatically added a using statement for
the WorldJourney.Models namespace as a result of you referencing
the City type:
19. using WorldJourney.Models;
20. In the CityController code window, ensure that the cursor is at the end of
the Details action code block, press Enter, and then type the following code:
21. public IActionResult GetImage()
22. {
23. }
24. In the GetImage action code block, type the following code:
25. ViewData["Message"] = "display Image";
26. City requestedCity = null;
27. if (requestedCity != null)
28. {
29. string fullPath = "";
30. FileStream fileOnDisk = new FileStream(fullPath, FileMode.Open);
31. byte[] fileBytes;
32. using (BinaryReader br = new BinaryReader(fileOnDisk))
33. {
34. fileBytes = br.ReadBytes((int)fileOnDisk.Length);
35. }
36. return File(fileBytes, requestedCity.ImageMimeType);
37. }
38. else
39. {
40. return NotFound();
41. }
Task 3: Change actions to get a parameter
1. In the CityController class code block, in the Details action code block, locate
the following code:
2. return View(city);
3. Place the mouse cursor before the located code, and type the following code:
4. ViewBag.Title = city.CityName;
Task 7: Run the application
Note: The browser displays the Index action result inside the City controller.
3. In Microsoft Edge, on the Earth image, click the London area. Note the red
arrow at the center of the Earth image (the map's hot area is not very accurate -
you may find you need to click over Scotland or Ireland!).
Note: The browser displays the Details action result inside the City controller.
4. In Microsoft Edge, click Close.
Results: After completing this exercise, you have created MVC controllers that
implement common actions for the City model class in the application.
Exercise 2: Configuring Routes by Using the Routing Table
Scenario
An important design priority for the application is that the visitors should be able to
easily and logically locate cities. To implement these priorities, you have been asked to
configure routes by using the routing table that enables the entry of user-friendly URLs
to access cities.
Note: In the next task you will register a new route with the routing table. Then,
you will not need to manually enter the Traveler/Index relative URL in the
address bar.
4. In Microsoft Edge, click Close.
Note: The name "Katie Bruce" shown in the title comes from the
new "TravelerRoute" route, registered in the routing table.
3. In Microsoft Edge, click Close.
Results: After completing this exercise, you will be able to register new custom routes
in the request pipeline for controllers in the application.
Exercise 3: Configuring Routes by Using Attributes
Scenario
In addition to configuring routes by using the routing table, you have been asked to
configure routes by using attributes as well to enable the entry of user-friendly URLs. In
this exercise you will use custom routes by means of attributes.
Task 1: Apply custom routes to a controller by using attributes
Results: After completing this exercise, you have added custom routes to
the City controller by using the Route attribute.
Exercise 4: Adding an Action Filter
Scenario
Your development team is new to ASP.NET Core MVC and is having difficulty in passing
the right parameters to controllers and actions. You need to implement a component
that displays the controller names and action names in an external file to help with this
problem. In this exercise, you will create an action filter for this purpose.
Note: The log files contain the output of the action filter.
Results: After completing this exercise, you have created an action filter class that logs
the details of actions, controllers, and parameters to an external log file whenever an
action is called.
01:27:111 hour and 27 minutes remaining on your lab session
Keyboard released from VM