In this section I add the ExceptionFilter, revert the JSON formatting to Pascal casing, and add the DeveloperExceptionPage.
- [Voiceover] It's now time to configure the HTTP Pipeline and needed services. Prior versions of ASP.net used several files from app start, the global ASAX and start-up to configure the application. This all has been compressed down into one file. By default it's called startup.cs and it's specified by the web host builder. The constructor loads the application configuration, we've already looked at that, and then it configures services used by the application.
This is also where you populate the dependency injection container, which we'll talk about in the next section. And then finally, it configures the HTTP Pipeline. ASP.net core is an opt-in model, so by default, nothing is available, and if you want to add in services like mvc or mvc core, which is a streamlined version of the mvc service just for restful services, static file support for any of those other items, you must opt in.
The app_start contains very little in a web API 2.2 project, really just the web API routing config, and authentication if it's being used. The routing table we use predominantly by asp.net and prior versions has largely been replaced with attribute routing in asp.net core so let's configure the application, migrating all those items over from the web API 2.2 into our asp.net core application.
As a review, here in program, this is where we specify on line 20, what the startup class is. So we open up start app, we've already looked at the constructor where it builds the configuration, pulls in those .json files, we want to do a couple of different things to the top of the file, first and foremost we're going to add in a local variable to hold the environment.
So the hosting environment that gets passed into the constructor let's us know whether we are in development, production, et cetera. But we want to have access to that throughout this class, so we will just simply add in a private, read-only Ihosting environment, and we'll just call it _env for simplicity. Then in the constructor, right after line 23, we're going to say 'env = env' and that's the only change we have to make to the constructor.
In the configure services method we have more work to do. The first thing we want to do is change 'add mvc' to 'add mvc core', this is a streamlined package that is designed specifically for building restful services, and then we want to configure our mvc core app. There's two things we want to do, the first thing is we want to add in our exception filter and that is done through a lambda, where we say 'config.filters.add' and that's going to be a new spy store exception filter, and we want to pass in, whether or not we are in development.
And there's that helper method right there for development, production and staging, if you are using your own syntax, then you would have to pull the environment for that specific string. You're going to need to add in a using statement in order to do this, and that is right here on line ten. Using spy store api.filters. Let's make this a little easier to read.
Now the other thing we want to do is add in a new json formatter. The reason for this, prior versions of asp.net web API return to json pascal cased. Well the world doesn't like that, it's camel cased pretty much in every other platform. Asp.net core has changed its default json to be camel cased like the rest of the internet. Well that's fine, unless you've got a legacy system that you're migrating and you want to make sure that you keep the same pascal casing that you had in the prior application.
Fortunately that's very easy to do, we add in a json formatter, and again that's another lambda. And we say the contract resolver equals new default contract resolver, now that name is confusing because the new default is camel cased, to set it back to pascal case, we set the contract resolver to the new default contract resolver.
I'm not responsible for naming it, it's just the way it is. And the other thing that we're going to add in just for readability is formatting, and we can make it indented, so we'll do formatting indented and that completes configuring asp.net mvc core. Now we scroll down to the configure method, the default template adds in some logging information.
Logging is beyond the scope of this course, and kicks off the mvc service with app.usemvc but we want to check that if we're in development, we want to get an enhanced exception experience. So very simply we can say if environment is development, then use developer exception page, and that sounds a little misleading, it's not actually a page when you're talking about services.
If you have this turned on and you're in development and there's some unhandled exception, perhaps there's an exception in our exception filter, then we get a much more robust message back from asp.net core than you normally would. Now if this was an mvc web application, it would be a much fancier error page than the standard error handler. Now you see there's no routing table in here, normally, you would add the routing table into this app.usemvc method call here, but because we are building a service, it doesn't need a routing table, we're going to be replacing the web API route config with attribute routing.
Released
8/3/2017- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services
Share this video
Embed this video
Video: Configure services and HTTP pipeline