QuizController

Let's move on to QuizController:

  1. From Solution Explorer, open the /Controllers/ folder.
  2. Right-click on the folder and select the usual Add | New Item command.
Ensure not to use the Add | Controller option available there, as it will activate a wizard-like feature that will also add some dependencies to our project, which is something we definitely don't need (yet).
  1. From the ASP.NET Core | Web treeview node, select Web API Controller Class; call the new file QuizController.cs and click on OK to have it added under the /Controllers/ folder, along with the already existing HomeController.cs and SampleDataController.cs, which we reviewed in Chapter 1, Getting Ready.

The controller will be created with a bunch of sample methods, which we'll not use. Delete the entire file content and replace it with the following code:

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

namespace TestMakerFreeWebApp.Controllers
{
[Route("api/[controller]")]
public class QuizController : Controller
{
// GET api/quiz/latest
[HttpGet("Latest/{num}")]
public IActionResult Latest(int num = 10)
{
var sampleQuizzes = new List<QuizViewModel>();

// add a first sample quiz
sampleQuizzes.Add(new QuizViewModel()
{
Id = 1,
Title = "Which Shingeki No Kyojin character are you?",
Description = "Anime-related personality test",
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});

// add a bunch of other sample quizzes
for (int i = 2; i <= num; i++)
{
sampleQuizzes.Add(new QuizViewModel()
{
Id = i,
Title = String.Format("Sample Quiz {0}", i),
Description = "This is a sample quiz",
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});
}

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

Let's take a quick look at the code to see what we've done.

As we can see, we started defining the Latest method accepting a single (optional) integer parameter value called num, which defaults to 10. The method accepts any GET request using the custom routing rules configured via the HttpGet attribute. This approach is called Attribute routing, and we'll be digging into it further later in this chapter. For now, let's stick to the code inside the method itself.

The behavior is really simple, since we don't (yet) have a Data Source; we're basically returning a couple of sample QuizViewModel objects. Note that, although it's just a fake response, we're doing it in a structured and credible way, respecting the number of items issued by the request and also providing different content for each one of them. As a matter of fact, we're basically following the same approach used by the SampleDataController.cs provided by our Visual Studio Angular SPA Template, which we looked up back in Chapter 1, Getting Ready.

It's also worth noting that we're using a JsonResult return type, which is the best thing we can do as long as we're working with ViewModel classes featuring the JsonObject attribute provided by the Newtonsoft.Json framework, that's definitely better than returning plain string or IEnumerable<string> types, as it will automatically take care of serializing the outcome and setting the appropriate response headers (Content-Type, charset, and so on).

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

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