Began to master ASP.NET Core. When trying to implement a simple application, the error crashes:

NullReferenceException: Object reference not set.

Controller:

[Route("")] public class ValuesController : Controller { // GET api/values [HttpGet] public IActionResult Get() { string[] files = {"wqew", "12232" }; return View("~/Index.cshtml", files); } } 

Index.cshtml file:

 @page @model string[] @{ } <ul> @foreach (string file in Model) { <li>@file</li> } </ul> 

As soon as the server starts, the above error appears on this line:

 @foreach (string file in Model) 
  • How to create and attach a view to the model? - One Two

1 answer 1

Remove the @page line and it will work.

I quote from the Razor dock :

The @page directive turns the file into an MVC action, and therefore processes requests directly, bypassing the controller.

  • Is it an alternative to static pages? - Anatol
  • @Anatol is not, you can run code on these pages, so this is more than a static page. This can be considered an alternative to the action without parameters, especially if you inject the necessary repositories / services into the view and make the actions thin. More details here Maiorov wrote, I have recently become addicted to this method. - AK