Creating the Post-Registration Landing Page

Follow these steps to create the post-registration landing page:

  1. We could use the same form page, but in this case, we will create SuccesfullyRegistered.cshtml in the Accounts folder, as follows:

  1. Use the following code:
@{
ViewBag.Title = "Registration Successful";
}
<h1>You have registered successfully!</h1>
<ul>
<li><a asp-action="" asp-controller="">Home</a></li>
<li><a asp-action="" asp-controller="">Login</a></li>
</ul>
  1. In the preceding code, we will fill in the necessary action and controller fields later. Although this page is simplistic, for the purpose of this demo, it is acceptable for the time being. The final step is to revise our controller, as follows:
using Microsoft.AspNetCore.Mvc;
using RestBuy.Application.Services;
using RestBuy.Application.ViewModels;
using System.Threading;
using System.Threading.Tasks;
namespace RestBuy.Web.Controllers
{
[Route("[controller]")]
...
...
}

We have added our IRegistrationService to the constructor. Our service will simply be injected by ASP.NET runtime once we have registered it against a concrete service, which we haven't written yet.

Secondly, we have defined a Register method that gets the ViewModel and a cancellation token. The cancellation token is entirely optional here. ASP.NET runtime has a smart behavior: whenever the HTTP request is aborted from the client side, it will trigger the cancellation token. Generally, it is recommended to pass the cancellation tokens for any I/O call. In this case, we will query the database, but feel free to skip it if you find this approach too verbose. Next, we will check whether our ViewModel is valid or not.

Remember that we have decorated our view model with few attributes. At the model binding phase, ASP.NET Runtime automatically validates our model and sets the model state accordingly. If our model is valid, then we proceed with the registration by calling the registration service and returning the view.

If our view is not valid, we will show the very same form page along with its validation errors, which are automatically displayed.

At this point, we recommend you write a unit test against the controller to verify the behavior, but we will skip that and directly implement our registration service. Our registration service has to check whether such a user exists in the database. So, let's first create a query for it, which will get implemented from IQuery<User>.