ResultController

Its corresponding ResultController is as follows:

using System; 
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using TestMakerFreeWebApp.ViewModels;
using System.Collections.Generic;

namespace TestMakerFreeWebApp.Controllers
{
[Route("api/[controller]")]
public class ResultController : Controller
{
// GET api/question/all
[HttpGet("All/{quizId}")]
public IActionResult All(int quizId)
{
var sampleResults = new List<ResultViewModel>();

// add a first sample result
sampleResults.Add(new ResultViewModel()
{
Id = 1,
QuizId = quizId,
Text = "What do you value most in your life?",
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});

// add a bunch of other sample results
for (int i = 2; i <= 5; i++)
{
sampleResults.Add(new ResultViewModel()
{
Id = i,
QuizId = quizId,
Text = String.Format("Sample Question {0}", i),
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});
}

// output the result in JSON format
return new JsonResult(
sampleResults,
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}
}
}

It's worth noting that the ResultViewModel and ResultController source code is much similar to QuestionViewModel and QuestionController. The reason for that is rather obvious--both Questions and Results are related to their Quiz without any intermediate relationship, such as the Answers have with the Questions.

Now that our .NET Core Controllers and ViewModels have been set in place, we can safely get rid of the SampleDataController.cs file, as we don't need it anymore. From Solution Explorer, navigate to the /Controllers/ folder, right-click on it and delete it. Doing this won't cause issues, as we already removed all the relevant client-side code back in Chapter 1, Getting Ready.

If we want to keep the SampleDataController.cs for further reference, we can also create a /Controllers/_deleted/ subfolder and move it there instead, just like we did with the counter and fetchdata Angular components back in Chapter 1, Getting Ready.

After the cleanup, let's take a clear look at the routing aspect of what we just did; since it is a major topic, it's well worth some of our time.

..................Content has been hidden....................

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