Rendering Views
So far we have been using plain C# classes as Controllers, but it is far more common have your Controllers inherit from the Controller
base class which the MVC framework provides. This allows developers to return complex objects from their Controllers, students in our case. These complex return types are returned in a result that implements the IActionResult
interface. We can, therefore, return JSON, XML, and even HTML to return to the client. The usage of this and creating Views is what we will be looking at next in this recipe.
Getting ready
Open up the StudentController
class and modify it to contain attribute-based routing. Be sure to add the using Microsoft.AspNetCore.Mvc;
namespace to the StudentController
class. Also, inherit from the Controller
base class.
[Route("Student")] public class StudentController : Controller { [Route("Find")] public string Find() { return "Found students"; } }
Then, add another folder to your project called Models
. Inside the...