How to do it...

  1. Open Visual Studio 2017.
  2. Now, open the solution from the previous recipe. Click File | Open | Open Project/Solutionor press Ctrl + Shift + Oand select the Chapter5.XmlDoc solution.
  3. Press Ctrl + Shift + B for a quick build to check that everything is fine. 
  4. Now, click on the Chapter5.XmlDoc solution label. Click File | Add | New Project.
  5. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. 
  1. Select Web and select ASP.NET Web Application (.NET Framework) in the right-hand pane:
  1. Now, in the Name: textbox, type Chapter5.XmlDoc.XmlMVC as the name and leave the Location: textbox at its default value:
  1. In the New ASP.NET Web Application dialog box, select Empty from the template list.
  1. Select MVC in the Add folders and core references for: option:
  1. Leave the rest as it is and click OK to create the default ASP.NET MVC web application template. 
  1. Now, the Solution Explorer should look like this: 
  1. Now, right-click on the References label under the Chapter5.XmlDoc.XmlMVC project and select Add Reference.
  1. In the Reference Manager dialog box, select Projects in the left-hand pane and select Chapter5.XmlDoc.XmLib in the right-hand pane:
  1. Click OK.
  2. Now, right-click on the Controllers folder inside the Chapter5.XmlDoc.XmlMVC project. 
  3. Select Add | Controller.
  1. In the Add Scaffold dialog box, select MVC 5 Controller – Empty from the template list and click Add:
  1. Now, in the Add Controller dialog box, type HomeController in the Controller name: textbox:
  1. Click Add.
  2. Now double-click on the HomeController.cs label under the Controllers folder.
  1. In the code window, right-click on the Index() method name and select Add View.
  2. Leave the defaults in the Add View dialog box and click Add:
  1. Click on the HomeController.cs tab in the code window.
  2. Add this using directive to the top of the code, next to the last line of the directives:
        using Chapter5.XmlDoc.XmlLib;
  1. 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");
  1. Press F5 to test our code and you should get an output like this:
  1. Now close the browser and, in the Solution Explorer, click on the Show All Files icon:
  1. Now you should see that the textlog.xml label is created and the Solution Explorer should look like this:
  1. Click on the testlog.xml label and you should see output similar to this:
  1. 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();
}
  1. Now, right-click on the Display() method name and select Add View.
  2. Follow step 22 to add the view.
  3. 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>
}
}
  1. 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.
  2. You should see output similar to this:
  1. Now close your browser and we are done.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.26.246