This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Getting Started with ASP.NET Core!
You have completed Getting Started with ASP.NET Core!
Preview
This video will take a closer look at the Startup class' constructor, ConfigureServices, and Configure methods and how they're used to configure our ASP.NET Core web app.
Additional Learning
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The startup class is just a plain class,
no base class inheritance, or
0:00
interface implementations.
0:05
It includes two specially named methods,
0:07
configure services, and configure.
0:11
Our host will look for and call these
methods when our app is starting up.
0:16
The constructor is being used to
set up our apps configuration.
0:21
App configuration is no longer
dependent upon the web config file.
0:25
Instead app configuration data,
stored as key value pairs,
0:30
can be read from a variety
of file formats.
0:35
We're adding three configuration
data sources, two JSON files and
0:38
environment variables.
0:43
The second JSON file is
being dynamically named
0:45
by including the environment
name before the file extension.
0:48
This allows us to customized
the configuration data for
0:52
each environment that we need to support.
0:55
The configuration data sources are loaded
in the order that they're defined.
0:58
If a key is the same as a key
that has already been loaded,
1:03
then the new value will
replace the old value.
1:06
This allows our environment
specific configuration settings
1:10
to override the base settings.
1:13
And our environment variables to
override any of the JSON settings.
1:15
Environment variables give us a convenient
way to provide sensitive configuration
1:20
data, like API keys or
database connection strings,
1:24
at runtime without having to
write those values to a file.
1:28
Instead, you define those
settings in your environment.
1:33
For instance,
in Microsoft's Azure Cloud Service,
1:36
you can define environment variables for
your web apps.
1:40
The ConfigureServices method is used to
configure any services that should be
1:42
made available to your app using the
built-in dependency injection container.
1:47
Here, we're calling the AddMVC extension
method to add the services that
1:52
MVC requires.
1:57
The configure method is used to configure
the HTTP request pipeline for our app.
1:59
Our request pipeline is
composed using middleware.
2:05
Each app.use method call that we see
here represents a middleware component
2:09
that is being added to the pipeline.
2:14
Earlier we saw a very basic example
of creating our own middleware.
2:17
Luckily, we don't need to create
all of our own middleware.
2:21
Each of these middleware components
is provided by ASP.NET Core,
2:24
the developer exception page,
browser link,
2:29
the exception handler,
static files and in MVC.
2:32
Notice that we're checking if
the current environment is development.
2:39
And if it is, then we add
the developer exception page and
2:42
browser link components.
2:46
If it isn't the development environment,
2:48
then we add the exception
handler component.
2:50
Each middleware component gets a chance
to perform asynchronous logic on the HTTP
2:53
context and optionally invoke
the next middleware in the sequence,
2:58
or terminate the requests directly.
3:04
For example, if the static files component
detects that the request is for a static
3:06
file, then the component will write
the file contents to the request body and
3:11
terminate the request, preventing
the MVC component from being invoked.
3:16
Let's try an experiment.
3:21
I'll comment out all of our request
pipeline configuration and run our app.
3:23
And we get a blank page.
3:38
If we open Chrome's dev tools and select
the Network tab and reload our page,
3:41
We can see that we're getting
a 404 Not Found HTTP status code.
3:49
No error page, just the status code.
3:55
This demonstrates that ASP.NET core uses a
build up approach to web app development.
3:58
Which is in contrast to
earlier versions of ASP.NET,
4:04
which used a tear down approach.
4:08
With the tear down approach,
ASP.NET provided a default feature set for
4:10
your app.
4:15
And if you didn't need or want one of
those features, you had to disable it.
4:16
Well, if you could it all.
4:20
With ASP.NET Core,
nothing is added by default.
4:22
You need to explicitly add the features
that you need for your app.
4:26
Let's add the MVC middleware
component back and run our app again.
4:36
Now we're getting the content for
our default route, but
4:47
it appears our styles and
images are missing.
4:50
In the Network tab,
4:57
we can see that all of those resources
are returning a 404 status code.
4:58
This is happening because by default,
ASP.NET Core won't serve static files.
5:03
If your app uses static files,
you need to explicitly add support for
5:10
static files by adding the static files
middleware component to your pipeline.
5:13
It's worth noting that if our app
didn't need support for static files,
5:28
we could remove that NuGet package
from our project.json file.
5:32
This would reduce the overall
deployment footprint for our app.
5:37
Let's add back support for
static files and
5:46
look at one more example of
ASP.NET Core's build up approach.
5:49
We'll throw an exception in our
HomeController's index action method and
5:59
run the app without debugging
by pressing Ctrl+F5.
6:06
And again, we don't get any content.
6:17
If we reload the page in the Network tab,
6:25
we'll see that we're getting a 500
Internal Server Error Status Code.
6:27
By default, ASP.NET Core doesn't
provide any exception handling.
6:37
But that can easily be added by adding
the developer exception page middleware
6:41
component.
6:46
Now when we run our app,
we get a nicely formatted exception page.
6:50
Or if we're running in production, we
can add the exception handler component,
7:07
which will log the exception.
7:12
Reset the request path through our
error route and re-execute the request.
7:14
Now we get a user friendly error page.
7:24
The startup class gives you an idea
of how much ASP.NET Core differs from
7:27
previous versions of ASP.NET.
7:32
In the next video, we'll take a look
at MVC controllers and views,
7:34
which have changed from previous versions
of MVC, but not to the same degree.
7:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up