How to add a built-in view to an Asp.net core 2.0 controller

Asked

Viewed 112 times

0

I created a view (Razor Page) in an Asp.Net Core 2.0 project. How I build it from scratch and I now need to assign a controller to it. How do I do it?

2 answers

3

Your View must be inside a folder with the same name as the Controller (but without the Controller. Let’s say your Controller be called CompanyController, the name of the folder View that Controller will just call Company) and within a folder called Views. Below we have an example.

Let’s say your View is inside a folder called Company:

inserir a descrição da imagem aqui

In this case, I have 5 views inside the briefcase Company.

Based on what I said above, you should then have a Controller called CompanyController and this must be inside a folder called Controller.

inserir a descrição da imagem aqui

Done that, inside your Controller, you’ll have something like this:

public class CompanyController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Where the method Index() is responsible for calling your View, by name Index, inside the briefcase Company.

0

Just create a class in the controller folder and place the page you want to render:

public class HomeController : Controller
{
    public IActionResult Home()
    {
        return View();
    }

} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.