上QQ阅读APP看书,第一时间看更新
Creating a view
To create a view, we first need to create the matching folder if it doesn't already exist, and then create the file. Follow these steps to accomplish that:
- On Solution Explorer, right-click on the project name and choose Add | New Folder.
- Set the name as Views and click Enter.
- Right-click on the newly created Views directory and, again, choose Add | New Folder.
- Set the new folder name as Home and click Enter.
- Right-click on the new Home folder and choose Add | View....
On the Add View dialog, do the following:
- Set the View name as Index.
- Choose Empty (without model) on the Template field.
- Uncheck the Use a layout page checkbox.
- Click Add:
- The new view has now been scaffolded for you.
- Find the <body></body> tags and change them to <body>I'm a fullstack developer!</body>.
Now that we have the view ready, we need to instruct the controller action to show the view:
- Open the HomeController class, which is in the Controllers/HomeController.cs file.
- Find the following line:
return Content("Hello from MVC!");
Replace it with this line:
return View();
- Run the application (press F5, or go to Debug | Start Debugging).
- The browser should open and look similar to the following screenshot:
- Congratulations! You have a working ASP.NET Core MVC view!
For now, this fits all of our SPA needs. We will dive into the HTML and view aspects more deeply when we discuss Angular in the next part of this book.