Exercise - Use built-in middleware
ASP.NET Core includes a set of built-in middleware components that handle common tasks, such as routing, authentication, and HTTP logging. You can use these components to add functionality to your app without writing custom middleware.
Your team lead tasked you to create a barebones website for your company. The website should display a welcome message on the main page, and display a brief history of the company on a separate /about
page. A previous version of the app had the company history at the /history
URL, so you need to redirect requests from /history
to /about
to maintain compatibility with existing links.
You'll use the built-in MapGet
method and UrlRewriter
middleware to accomplish these tasks.
Create an ASP.NET Core app
You need an ASP.NET Core app to play the role of your team's app. Let's create a new ASP.NET Core app using the C# Dev Kit extension in Visual Studio Code.
Launch Visual Studio Code.
Press Ctrl+Shift+P to open the command palette.
Search for and select .NET: New Project....
Search for and select ASP.NET Core Empty.
Select or create a folder for the new project.
Name the new app MyWebApp.
Select Create project to create the project.
When the new project opens, expand the
Solution Explorer
pane to view the project files.
Run the app
Test the app to make sure it runs.
In Visual Studio Code, press F5 to build and run the app.
- When prompted, select C# as the debugger.
- When prompted, select C#: MyWebApp [Default Configuration] as the launch configuration to use.
This command starts the app and hosts it on a local web server. A browser window opens and displays, "Hello, World!"
Close the browser window and stop the app by pressing Shift+F5 in Visual Studio Code.
Map endpoints and add a URL rewriter
Now that you have a working app, let's add a welcome message to the main page.
Open the Program.cs file.
On the
app.MapGet("/", () => "Hello World!");
line, change the "Hello World!" message to "Welcome to Contoso!"app.MapGet("/", () => "Welcome to Contoso!");
app.MapGet()
maps an HTTP GET request to a specified path. This feature of ASP.NET Core is called endpoint routing. This code adds a branch to the pipeline. If the request path is/
, the endpoint routing middleware routes the request to this endpoint, which then writes "Welcome to Contoso!" to the response.On the next line, add the following code:
app.MapGet("/about", () => "Contoso was founded in 2000.");
This code adds another endpoint. If the request path is
/about
, the endpoint writes "Our company was founded in 2000." to the response.Before the first
app.MapGet()
, add the following code:app.UseRewriter(new RewriteOptions().AddRedirect("history", "about"));
This code adds a URL rewriter middleware component that redirects requests from
/history
to/about
. TheAddRedirect()
method takes two parameters: a regular expression pattern to match the request path, and the replacement path to redirect to.Add the following directive to the top of the file:
using Microsoft.AspNetCore.Rewrite;
This directive resolves the reference to the
RewriteOptions
class.
Test the changes
- Save all your changes and run the app as before.
- When the browser window opens, note the root URL displays "Welcome to Contoso!" Add
/about
to the URL and press Enter. The browser should display "Contoso was founded in 2000." - Replace
/about
with/history
in the URL and press Enter. The browser should redirect to/about
. - Leave the app running for the next exercise.