Join Mike Benkovich for an in-depth discussion in this video An MVC frame of mind, part of ASP.NET MVC 5 Essential Training (2018).
- [Instructor] Web development is different than Windows or Rich Client development in that we've got a request that comes from the client machine, gets sent to the website, and then it gets processed. The basic flow of things is the user enters the URL in their own browser, it gets sent across the wire to the website, which then takes it, processes it, generates a response, and then goes out and sends back a response. This is a process that is pretty well-known, and over time, there's been a number of different types of frameworks and technologies that have come along to simplify that process, taking us from the old days when it was creating a listener to handle that process with a lot of spaghetti code to Active Server Pages, to Web Forms, PHP, where each of these different types of approaches allowed us to create a logical model around, how do we get the request and generate that response and send it back? Some of these patterns include things like the Model-View-Presenter, which is common in a Rich Client, like a Windows application, where the pattern is to have an event-based type of an architecture where I drag a button onto a form, double-click on it, add logic of what it does, and then go to the next page and add another button, and you end up creating code all over the place.
The Model-View-Adapter is another model, MVVM, which is common in mobile applications, as well as MVC, which is a pretty common pattern for building web applications, and the reason why is there's a lot of great advantages to it. What is Model-View-Controller, MVC? It is really taking the process of focusing and separating out the entire process of taking that request and looking at it from three areas: the Controller, which gets the request, routes it to the right spot, calls the right logic, the View, which has the presentation of it, and if we have data, then we've got a Model that we can use for making sure that we're working with the right business logic on how we want to handle the processing with that.
So, what are some of the benefits that this gives us? You might be thinking, well, why do I want to go out and learn a whole new way of doing this? There's quite a few benefits, including separations of concerns, which allow us to break the problem down into separate areas, if we're collaborating with other people, to have a team that is able to look at the presentation layer, to look at the data layer, to look at how we're doing our routing. The separation of concerns leads into how we can also enhance the testability of the application by breaking it into littler pieces that can be tested, and then we can use that to confirm that our application does what it's supposed to do.
We get real good control over the HTML that is being rendered, and it's a nice way to get down to that low level. So, if this is the first time you're coming into looking at MVC, you might be thinking, this is really quite a bit different than what I've done before, especially if you're coming from something like Web Forms where I've got code in different places. I don't have to worry about creating controllers for logic. If I want to add pages, I just add a folder. With MVC, I have to be thinking ahead of time on how I want to route things around.
There's a lot of concepts that you have to learn, things like the model binding and validation, the filters that I can put onto actions. I've got scaffolding. So, how do I go out and create new pages? Well, there's some things in MVC that it does that is really nice, that you couldn't do in Web Forms. There's also the dependency injection, which is a concept where we want to make it easier for us to be able to go out and test and break out the logic so that we can have a way to make sure that our code does what we want it to do.
Some of the more advanced concepts that we'll talk about in this course is talking about things like the HTTP programming model, how we get closer to the requests and the responses, and how we look at the HTTP status codes that come back. We think about the REST pattern, with actions dispatching based on verbs. There is also the opportunity to work with concent negotiation, so that when a request comes in, we'll return back a response that maps out to what the client wants. It might be JSON, it might be data, it might be XML as opposed to just HTML.
Configuration based on code, which is where we want to allow us to configure how our application works. There's bundling and minification, which are efficiencies that we can get in how we're rendering data back and forth. MVC is not a completely new way of doing everything. A lot of these concepts also exist inside of Web Forms, and as the framework has evolved, these are coming in there so that you can use and leverage these different types of tools in a given app. But it's not meant to be something that is just so complex that you don't have time to learn it.
It's meant to simplify and make it easier for us to go out and build great software. MVC is a great way to organize your code, to get into the markup and the control flow, and it is fairly easy to learn if you've got the basic background in how .NET works and are familiar with the web patterns. So, with that, let's dive into building our first app.
- Creating a new ASP.NET MVC 5 project
- Building custom routes
- Creating custom layouts
- Adding a model to a view
- Deploying to Azure and Amazon Web Services
- Configuring authentication and authorization
- Unit testing code