Now, open the solution from the previous recipe. ClickFile |Open |Open Project/Solution, or pressCtrl + Shift + O, and select the Chapter5.XmlDoc solution.
PressCtrl+Shift+Bfor a quick build to check that everything is fine.
Now, click on the Chapter5.XmlDoc solution label. ClickFile |Add |New Project.
In theAdd New Projecttemplate dialog box, expand the Visual C# node in the left-hand pane.
Select Web and select ASP.NET Web Application (.NET Framework) in the right-hand pane:
Now, in the Name: textbox, type Chapter5.XmlDoc.XmlMVC as the name and leave the Location: textbox at its default value:
In the New ASP.NET Web Application dialog box, select Empty from the template list.
Select MVC in the Add folders and core references for: option:
Leave the rest as it is and click OK to create the default ASP.NET MVC web application template.
Now, the Solution Explorer should look like this:
Now, right-click on the References label under the Chapter5.XmlDoc.XmlMVC project and select Add Reference.
In the Reference Manager dialog box, select Projects in the left-hand pane and select Chapter5.XmlDoc.XmLib in the right-hand pane:
Click OK.
Now, right-click on the Controllers folder inside the Chapter5.XmlDoc.XmlMVC project.
Select Add | Controller.
In the Add Scaffold dialog box, select MVC 5 Controller – Empty from the template list and click Add:
Now, in the Add Controller dialog box, type HomeController in the Controller name: textbox:
Click Add.
Now double-click on the HomeController.cs label under the Controllers folder.
In the code window, right-click on the Index() method name and select Add View.
Leave the defaults in the Add View dialog box and click Add:
Click on the HomeController.cs tab in the code window.
Add this using directive to the top of the code, next to the last line of the directives:
using Chapter5.XmlDoc.XmlLib;
Now, let's add this code inside the Index() method and before the return statement:
var xmlFile = $"{Server.MapPath("~")}/testlog.xml";
var xmlLog = new XMLLog(xmlFile); xmlLog.WriteToLog("Start at the Index() method"); xmlLog.WriteToLog("Another log entry here"); xmlLog.WriteToLog("Before the return statement");
Press F5 to test our code and you should get an output like this:
Now close the browser and, in the Solution Explorer, click on the Show All Files icon:
Now you should see that the textlog.xml label is created and the Solution Explorer should look like this:
Click on the testlog.xml label and you should see output similar to this:
Now, let's click on the HomeController.cs tab and add this code next to the Index() method:
public ActionResult Display() {
var xmlFile = $"{Server.MapPath("~")}/testlog.xml"; var xmlLog = new XMLLog(xmlFile);
ViewBag.LogDetails = xmlLog.ReadLog();
return View(); }
Now, right-click on the Display() method name and select Add View.
Follow step 22 to add the view.
Now, in Display.cshtml, add the following code next to the <h2> tags:
@{
var xmlLogDetails = (Dictionary<string, string>)ViewBag.LogDetails;
foreach (var log in xmlLogDetails) { <p>@log.Key.Split('-')[0]: @log.Value.Split('-')[0]</p> } }
Now, let's press F5 to debug the code. By default, this should load Display.chtml in the browser; if not, type http://locahost<portnumber/Home/Display and press Enter.