Adding more routes

Let's get back to our QuizController. Now that we're aware of the various routing patterns available, we can use them to implement the API calls we're still missing.

Open the QuizController.cs file and add the following code (new lines are highlighted):

 
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
{
#region RESTful conventions methods
/// <summary>
/// GET: api/quiz/{}id
/// Retrieves the Quiz with the given {id}
/// </summary>
/// <param name="id">The ID of an existing Quiz</param>
/// <returns>the Quiz with the given {id}</returns>
[HttpGet("{id}")]
public IActionResult Get(int id)
{
// create a sample quiz to match the given request
var v = new QuizViewModel()
{
Id = id,
Title = String.Format("Sample quiz with id {0}", id),
Description = "Not a real quiz: it's just a sample!",
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
};

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

#region Attribute-based routing methods
/// <summary>
/// GET: api/quiz/latest
/// Retrieves the {num} latest Quizzes
/// </summary>
/// <param name="num">the number of quizzes to retrieve</param>
/// <returns>the {num} latest Quizzes</returns>
[HttpGet("Latest/{num}")]
public IActionResult Latest(int num = 10)
{
var sampleTests = 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
});
}
#endregion
}
}

As we can see, we did a bunch of significant improvements here:

  • We added a Get method that follows the RESTful conventions we explained earlier; we will definitely need it each time we'll have to retrieve a specific Quiz, given its Id.
  • We also decorated each class member with a dedicated <summary> documentation tag explaining what it does and its return value. These tags will be used by IntelliSense to show real-time information about the type within the Visual Studio GUI. They will also come inhandy when we'll want to generate an autogenerated XML Documentation for our project using industry-standard documentation tools, such as Sandcastle.
  • Finally, we added some #region / #endregion pre-processor directives to separate our code into blocks. We'll do this a lot from now on, as this will greatly increase the readability and usability of our source code, allowing us to expand or collapse different sections/parts when we don't need them, thus focusing more on what we're working with.
For more information regarding documentation tags, take a look at the MSDN official documentation page, at https://msdn.microsoft.com/library/2d6dt3kf.aspx.

If you want know more about C# pre-processor directives, check out https://msdn.microsoft.com/library/9a1ybwek.aspx instead.
..................Content has been hidden....................

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