Overview of Model View Controller (MVC)
Model View Controller (MVC) is a framework for building web applications using a specified design.
- M stands for model.
- V stands for view.
- C stands for controller.
ModelModel represents the part of the application that handles the logic for application data. Mainly the Model object retrieves and stores data in a database.
Views
Views are that part of an application that handles the display of data. Views are created from model data.
Controller
The Controller is the part of the application that handles user interaction. It is the bridge between the Model and the View. Typically the Controller reads the data from the View controls the user input and sends input data to the model.
What is new in ASP.NET MVC 6?
Full Framework vs. Cloud-optimized Framework
In MVC 6 Microsoft removed the dependency of System.Web.Dll from MVC6 because it's so expensive that typically it consumes 30k of memory per request and response, whereas now MVC 6 only requires 2k of memory per request and the response is very small memory consumption.
The advantage of using the cloud-optimized framework is that we can include a copy of the mono CLR with your website. For the sake of one website we do not need to upgrade the .NET version on the entire machine. A different version of the CLR for a different website running side by side.
MVC 6 is a part of ASP.NET 5 that has been designed for cloud-optimized applications. The runtime automatically picks the correct version of the library when our MVC application is deployed to the cloud.
The Core CLR is also supposed to be tuned with a highly resource-efficient optimization.
Microsoft has made many MVC, Web API, WebPage, and SignalLr pieces we call MVC 6.
Most of the problems are solved using the Roslyn Compiler. In ASP.NET vNext uses the Roslyn Compiler. Using the Roslyn Compiler we do not need to compile the application, it automatically compiles the application code. You will edit a code file and can then see the changes by refreshing the browser without stopping or rebuilding the project.
Run on hosts other than IIS
Where we use MVC5 we can host it on an IIS server and we can also run it on top of an ASP. Net Pipeline, on the other hand, MVC 6 has a feature that makes it better and that feature is itself hosted on an IIS server and a self-user pipeline.
Environment based configuration system
The configuration system provides an environment to easily deploy the application on the cloud. Our application works just like a configuration provider. It helps to retrieve the value from the various configuration sources like XML files.
MVC6 includes a new environment-based configuration system. Unlike something else, it depends on just the Web. Config file in the previous version.
Dependency injection
Using the IServiceProvider interface we can easily add our own dependency injection container. We can replace the default implementation with our own container.
Supports OWIN
We have complete control over the composable pipeline in MVC 6 applications. MVC 6 supports the OWIN abstraction.
Important components of an MVC6 application
The is a new file type in MVC6 as in the following.
The preceding files are new in MVC 6. Let's see what each of these files contains.
Config.Json
This file contains the application configuration in various places. We can define our application configuration, not just this file. There is no need to be concerned about how to connect to various sources to get the confutation value.
In the following code, we add a connection string in the Config.json file.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=server_name;Database=database_name;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
Project.json
This file contains the build information as well as project dependencies. It can contain the commands used by the application.
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta1",
"Microsoft.AspNet.Server.IIS": "1.0.0-alpha4"
},
"commands": {
/* Change the port number when you are self hosting this application */
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"frameworks": {
"aspnet50": {},
"aspnetcore50": {}
}
}
Startup.cs
The Application Builder is used as a parameter when the configure method is used when the Startup class is used by the host.
Global.json
Define the location for the project reference so that the projects can reference each other.
Defining the Request Pipeline
If we look at the Startup.cs it contains a startup method.
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
var Configuration = new Configuration();
Configuration.AddJsonFile("config.json");
Configuration.AddEnvironmentVariables();
}
Create a Controller
If we look at the controller it is similar to the controller in a MVC 5 application.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
The namespace for MVC6 is Microsoft.ASPNET.MVC, unlike the System.Web.MVC in the previous version. That is a big difference. In MVC 6 the application does not need to be derived from the controller class. In our controller, there are many default functionalities. Most of the time we might need to derive from the controller class, but if we do not need access to all of the functionally provided by the controller class, we can define our controller class.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public string atul()
{
return "hello i am mvc6";
}
}
If you execute the preceding method we may see the following page.
Summary
In this article, we have learned about the new features of MVC 6. We have now become familiar with MVC 6.