Activity 3 - Create a Web App

Instructions

Creating a Project

  1. Ensure that you have Visual Studio Code open.

  2. On the Activity bar, select Explorer.

  3. Select the Create .NET Project button.

    The command pallette will drop down a list of project types to select from.

  4. From the list, select Blazor Server App

  5. Select the folder you would like your new app to be stored in.

  6. Give it a name.

    The app will be created and a Solution menu will appear in the Explorer.

    Screenshot of Solution Explorer in VS Code

You now have a website!

Review the Razor Pages project structure

The following table describes the project structure that was generated.

NameDescription
Pages/Contains Razor Pages and supporting files. Each Razor page has a .cshtml file and a .cshtml.cs PageModel class file.
wwwroot/Contains static asset files like HTML, JavaScript, and CSS.
ContosoPizza.csprojContains project configuration metadata, such as dependencies.
Program.csServes as the app’s entry point and configures app behavior, like routing.

Other noteworthy observations:

URLMaps to Razor page
www.domain.comPages/Index.cshtml
www.domain.com/IndexPages/Index.cshtml
www.domain.com/PrivacyPages/Privacy.cshtml
www.domain.com/ErrorPages/Error.cshtml

Subfolders in the Pages directory are used to organize Razor pages. For example, if there were a Pages/Products directory, the URLs would reflect that structure:

URLMaps to Razor page
www.domain.com/ProductsPages/Products/Index.cshtml
www.domain.com/Products/IndexPages/Products/Index.cshtml
www.domain.com/Products/CreatePages/Products/Create.cshtml

There are several files that are shared across multiple pages. These files determine common layout elements and page imports. The following table describes the purpose of each file.

FileDescription
_ViewImports.cshtmlImports namespaces and classes that are used across multiple pages.
_ViewStart.cshtmlSpecifies the default layout for all Razor pages.
Pages/Shared/_Layout.cshtmlThis is the layout specified by the _ViewStart.cshtml file. Implements common layout elements across multiple pages.
Pages/Shared/_ValidationScriptsPartial.cshtmlProvides validation functionality to all pages.

Run your project

  1. Ensure that you have Visual Studio Code open and in the Activity bar, Explorer is selected.

  2. In the Solution Explorer, right click on your project name, select Debug and select Start new instance.

    This will build and launch your new website.

    Screenshot of the website you just created
  3. Click on Counter and Fetch Data links to navigate around your website.

Customize the landing page

Let’s make a few changes to the landing page to make it more relevant to the your app.

  1. In Pages/Index.razor, add a block of C# code with the following code:
@code {
TimeSpan timeInBusiness = DateTime.Now - new DateTime(2010, 01, 17);
}

The preceding code:

  1. Change the PageTitle element to say “Welcome to my first web page”

  2. Change the h1 to say “Welcome to my first Razor web app”

  3. Replace the remaining text with the following code:

<p class="lead">The best website in town for @Convert.ToInt32(timeInBusiness.TotalDays) days!</p>

The preceding code:

  1. Save the file. Refresh the browser tab with the app to display the changes.
Screenshot of site after changes from Activity 3