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 Models folder, add a class called Student because our application will be returning student information. This will be a simple class with properties for the student number, first name, and last name. Your Student class should look as follows:

public class Student
{
public int StudentNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

Back in the StudentController, we want to instantiate our Student model and give it some data. We then change the return type of the Find() method from string to IActionResult. Also, add the using AspNetCore.Models; namespace to your StudentController class.

Note, if your project is called something other than AspNetCore, your namespace will change accordingly:
using [projectname].Models;

Your code should now look as follows:

[Route("Find")]
public IActionResult Find()
{
var studentModel = new Student
{
StudentNumber = 123
, FirstName = "Dirk"
, LastName = 'Strauss"
};
return View(studentModel);
}

Ultimately, we want to return a view result from our StudentController. We now have everything set up to do that next.

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

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