Chapter 1: Understanding the ASP.NET 5 React Template
React was Facebook's answer to helping more people work on the Facebook code base and deliver features quicker. React worked so well for Facebook that they eventually open sourced it (https://github.com/facebook/react). Today, React is a mature library for building component-based frontends (client-side code that runs in the browser); it is extremely popular and has a massive community and ecosystem. At the time of writing, React is downloaded over 8.8 million times per week, which is 2 million more than the same time a year ago.
ASP.NET Core was first released in 2016 and is now a mature open source and cross-platform web application framework. It's an excellent choice for building backends (application code that runs on the server) that interact with databases such as SQL Server. It also works well in cloud platforms such as Microsoft Azure.
In this first chapter, we'll start by learning about the...
Technical requirements
We will need to use the following tools in this chapter:
- Visual Studio 2019: This can be downloaded and installed from https://visualstudio.microsoft.com/vs/. Make sure that the following features are selected in the installer:
a) ASP.NET and web development
b) Azure development
c) Node.js development
- .NET 5.0: This can be downloaded from https://dotnet.microsoft.com/download/dotnet/5.0.
- Node.js and npm: These can be downloaded from https://nodejs.org/.
All the code snippets in this chapter can be found online at https://github.com/PacktPublishing/ASP.NET-Core-5-and-React-Second-Edition.
Check out the following video to see the code in action: https://bit.ly/3riGWib.
SPA architecture
In this section, we will start to understand the single-page application (SPA) architecture.
A SPA is a web app that loads a single HTML page that is dynamically updated by JavaScript as the user interacts with the app. Imagine a simple sign-up form where a user can enter a name and an email address. When the user fills out and submits the form, a whole page refresh doesn't occur. Instead, some JavaScript in the browser handles the form submission with an HTTP POST
request and then updates the page with the result of the request. Refer to the following diagram:
Figure 1.1 – Form in a SPA
So, after the first HTTP request that returns the single HTML page, subsequent HTTP requests are only for data and not HTML markup. All the pages are rendered in the client's browser by JavaScript.
So, how are different pages with different URL paths handled? For example, if I enter https://qanda...
Understanding the ASP.NET Core backend
In this section, we are going to start by creating an ASP.NET Core and React app using the standard template in Visual Studio. This template is perfect for us to review and understand basic backend components in an ASP.NET Core SPA.
Once we have scaffolded the app using the Visual Studio template, we will inspect the ASP.NET Core code, starting from its entry point. During our inspection, we will learn how the request/response pipeline is configured and how requests to endpoints are handled.
Creating an ASP.NET Core and React templated app
Let's open Visual Studio and carry out the following steps to create our templated app:
- In the start-up dialog, choose Create a new project:
Figure 1.2 – Visual Studio start-up dialog
- Next, choose ASP.NET Core Web Application in the wizard that opens and click the Next button:
Figure 1.3 – Creating a new...
Understanding the React frontend
It's time to turn our attention to the React frontend. In this section, we'll inspect the frontend code, starting with the entry point, which is a single HTML page. We will explore how the frontend is executed in development mode and how it is built in preparation for deployment. We will then learn how the frontend dependencies are managed and also understand why it took over a minute to run the app for the first time. Finally, we will explore how React components fit together and how they access the ASP.NET Core backend.
Understanding the frontend entry point
We have a good clue as to where the entry point is from our examination of the Startup
class in the ASP.NET Core backend. In the Configure
method, the SPA middleware is set up with the source path set to ClientApp
:
app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment...
Summary
In this chapter, we started off by learning that all the pages in a SPA are rendered in JavaScript with the help of a framework such as React, along with requests for data. This is handled by a backend API with the help of a framework such as ASP.NET Core. We now understand that the Startup
class configures services that are used in the ASP.NET Core backend, as well as the request/response pipeline. Requests to specific backend API resources are handled by controller classes.
We also saw how CRA was leveraged by the ASP.NET Core React template to create the React app. This tool did a huge amount of setup and configuration for us, including creating a development server, bundling, linting, and even creating key polyfills for IE. We learned that the React app lives in the ClientApp
folder in an ASP.NET Core React templated project, with a file called index.html
being the single page. A file called package.json
...
Questions
Have a go at answering the following questions to test the knowledge that you have acquired in this chapter:
- What is the entry point method in an ASP.NET Core app?
- What is the single HTML page filename in an ASP.NET Core React app that's created by a template? What folder is this located in?
- What file are React app dependencies defined in?
- What
npm
command will run the React app in the Webpack development server? - What
npm
command builds the React app so that it's ready for production? - What is the method name in a React class component that renders the component?
- Have a look at the following code snippet, which configures the request/response pipeline in an ASP.NET Core app:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseAuthentication(); app.UseHttpsRedirection(); app.UseMvc(); }
Which is invoked first in the request/response pipeline...
Answers
- A method called
Main
in theProgram
class is the entry point method in an ASP.NET Core app. - A file called
index.html
is the single HTML page filename. This is located in thepublic
folder, which can be found in theClientApp
folder. - The React app dependencies are defined in a file called
package.json
in theClientApp
folder. npm start
is the command that will run the React app in the WebPack development server.npm run build
is the command that builds the React app so that it's ready for production.- The
render
method renders a React class component. - Authentication will be invoked first in the request/response pipeline.
- We can give the
Startup
class a different name by defining this class inIHostBuilder
, as shown in the following example:public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) ...
Further reading
The following are some useful links so that you can learn more about the topics that were covered in this chapter:
- ASP.NET Core startup: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
- ASP.NET Core web API controllers: https://docs.microsoft.com/en-us/aspnet/core/web-api
- Create React app: https://facebook.github.io/create-react-app/
- WebPack development server: https://webpack.js.org/configuration/dev-server/
- npm: https://docs.npmjs.com/
- JSX: https://reactjs.org/docs/introducing-jsx.html
- JavaScript module import: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
- JavaScript module export: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
- JavaScript fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
- JavaScript array map: https://developer.mozilla.org/en-US/docs/Web/JavaScript...