--- title: Adding a View to an MVC app author: Rick-Anderson description: Adding a view to an MVC app ms.author: riande manager: wpickett ms.date: 09/1721/2017 ms.topic: article ms.technology: dotnet-mvc ms.prod: .net-framework uid: mvc/overview/getting-started/introduction/adding-a-view --- Adding a View ==================== by [Rick Anderson](https://github.com/Rick-Anderson) In this section you're going to modify the `HelloWorldController` class to use view template files to cleanly encapsulate the process of generating HTML responses to a client. You'll create a view template file using the [Razor view engine](../../../../web-pages/overview/getting-started/introducing-razor-syntax-c.md). Razor-based view templates have a *.cshtml* file extension, and provide an elegant way to create HTML output using C#. Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow. Currently the `Index` method returns a string with a message that is hard-coded in the controller class. Change the `Index` method to return a `View` object, as shown in the following code: [!code-csharp[Main](adding-a-view/samples/sample1.cs?highlight=1,3)] The `Index` method above uses a view template to generate an HTML response to the browser. Controller methods (also known as [action methods](http://rachelappel.com/asp.net-mvc-actionresults-explained)), such as the `Index` method above, generally return an [ActionResult](https://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult.aspx) (or a class derived from [ActionResult](https://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult.aspx)), not primitive types like string. Right click the *Views\HelloWorld* folder and click **Add**, then click **MVC 5 View Page with (Layout Razor)**. ![](adding-a-view/_static/image1.png) In the **Specify Name for Item** dialog box, enter *Index*, and then click **OK**. ![](adding-a-view/_static/image2.png) In the **Select a Layout Page** dialog, accept the default **\_Layout.cshtml** and click **OK**. ![](adding-a-view/_static/image3.png) In the dialog above, the *Views\Shared* folder is selected in the left pane. If you had a custom layout file in another folder, you could select it. We'll talk about the layout file later in the tutorial The *MvcMovie\Views\HelloWorld\Index.cshtml* file is created. ![](adding-a-view/_static/image4.png) Add the following highlighted markup. [!code-cshtml[Main](adding-a-view/samples/sample2.cshtml?highlight=4-11)] Right click the *Index.cshtml* file and select **View in Browser**. ![PI](adding-a-view/_static/image5.png) You can also right click the *Index.cshtml* file and select **View in Page Inspector.** See the [Page Inspector tutorial](../../views/using-page-inspector-in-aspnet-mvc.md) for more information. Alternatively, run the application and browse to the `HelloWorld` controller (`http://localhost:xxxx/HelloWorld`). The `Index` method in your controller didn't do much work; it simply ran the statement `return View()`, which specified that the method should use a view template file to render a response to the browser. Because you didn't explicitly specify the name of the view template file to use, ASP.NET MVC defaulted to using the *Index.cshtml* view file in the *\Views\HelloWorld* folder. The image below shows the string "Hello from our View Template!" hard-coded in the view. ![](adding-a-view/_static/image6.png) Looks pretty good. However, notice that the browser's title bar shows "Index - My ASP.NET Appli" and the big link on the top of the page says "Application name." Depending on how small you make your browser window, you might need to click the three bars in the upper right to see the to the **Home**, **About**, **Contact**, **Register** and **Log in** links. ## Changing Views and Layout Pages First, you want to change the "Application name" link at the top of the page. That text is common to every page. It's actually implemented in only one place in the project, even though it appears on every page in the application. Go to the */Views/Shared* folder in **Solution Explorer** and open the *\_Layout.cshtml* file. This file is called a *layout page* and it's in the shared folder that all other pages use. ![_LayoutCshtml](adding-a-view/_static/image7.png) Layout templates allow you to specify the HTML container layout of your site in one place and then apply it across multiple pages in your site. Find the `@RenderBody()` line. `RenderBody` is a placeholder where all the view-specific pages you create show up, "wrapped" in the layout page. For example, if you select the **About** link, the *Views\Home\About.cshtml* view is rendered inside the `RenderBody` method. Change the contents of the title element. Change the [ActionLink](https://msdn.microsoft.com/en-us/library/dd504972(v=vs.108).aspx) in the layout template from "Application name" to "MVC Movie" and the controller from `Home` to `Movies`. The complete layout file is shown below: [!code-cshtml[Main](adding-a-view/samples/sample3.cshtml?highlight=6,20)] Run the application and notice that it now says "MVC Movie ". Click the **About** link, and you see how that page shows "MVC Movie", too. We were able to make the change once in the layout template and have all pages on the site reflect the new title. ![](adding-a-view/_static/image8.png) When we first created the *Views\HelloWorld\Index.cshtml* file, it contained the following code: [!code-cshtml[Main](adding-a-view/samples/sample4.cshtml)] The Razor code above is explicitly setting the layout page. Examine the *Views\\_ViewStart.cshtml* file, it contains the exact same Razor markup. The *[Views\\_ViewStart.cshtml](https://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx)* file defines the common layout that all views will use, therefore you can comment out or remove that code from the *Views\HelloWorld\Index.cshtml* file. [!code-cshtml[Main](adding-a-view/samples/sample5.cshtml?highlight=1-3)] You can use the `Layout` property to set a different layout view, or set it to `null` so no layout file will be used. Now, let's change the title of the Index view. Open *MvcMovie\Views\HelloWorld\Index.cshtml*. There are two places to make a change: first, the text that appears in the title of the browser, and then in the secondary header (the `