AspNetCore.Docs/aspnetcore/includes/mvc-intro/adding_view1.md

14 lines
1.1 KiB
Markdown
Raw Normal View History

# Adding a view to an ASP.NET Core MVC app
2017-03-30 09:56:36 +08:00
By [Rick Anderson](https://twitter.com/RickAndMSFT)
In this section you modify the `HelloWorldController` class to use Razor view template files to cleanly encapsulate the process of generating HTML responses to a client.
2017-03-30 09:56:36 +08:00
You create a view template file using Razor. Razor-based view templates have a *.cshtml* file extension. They provide an elegant way to create HTML output using C#.
2017-03-30 09:56:36 +08:00
Currently the `Index` method returns a string with a message that's hard-coded in the controller class. In the `HelloWorldController` class, replace the `Index` method with the following code:
2017-03-30 09:56:36 +08:00
[!code-csharp[](../../tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Controllers/HelloWorldController.cs?name=snippet_4)]
2017-03-30 09:56:36 +08:00
2017-11-09 08:30:14 +08:00
The preceding code returns a `View` object. It uses a view template to generate an HTML response to the browser. Controller methods (also known as action methods) such as the `Index` method above, generally return an [IActionResult](https://docs.microsoft.com/aspnet/core/api/microsoft.aspnetcore.mvc.iactionresult) (or a class derived from `ActionResult`), not a type like string.