Introduction
ASP.NET 5 is the next version of ASP.NET,it was previously called ASP.NET vNext.
MVC 6 is part of ASP.NET 5. As we can expect with any new version of a framework many changes are introduced but lot more concepts and changes are introduced in MVC 6.This is because the underlying ASP.NET framework has been rebuild from the ground up so it reflects in the MVC framework as well.
We have used the following frameworks for different scenarios.
Framework | Useful in scenario |
---|---|
MVC | Web applications which have separation of concerns and are testable. |
WEB API | Web services that target different types of devices |
WEB PAGES | It's Lightweight framework for building smaller web applications |
Some Disadvantages of MVC 5
These frameworks serves different purposes perfectly. When working with these frameworks we realize that conceptually they are very similar.
MVC and WebAPI have lots of concepts in common like
Controllers Actions Filters Model binding
But still each uses entirely different types in different namespaces and assemblies. Below are a few examples:
WebAPI and MVC both have controllers and the purpose of controllers in both of these frameworks is to handle the incoming HTTPRequest. But the WebAPI controllers inherit from the ApiController class where as the MVC controllers inherit from the Controller class.
WebPages and MVC both are used to build UI applications and both use HTMLHelpers for creating UI functionality. But the implemention of the HTMLHelpers is entirely different.
For WebPages HTML helpers are defined in System.Web.WebPages.Html namespace in System.Web.WebPages assembly. For MVC HTML helpers are defined in System.Web.Mvc namespace in the System.Web.Mvc assembly.
The different implementations of these frameworks has the following disadvantages.
We have to learn about the different types in different namespaces even though there purpose is same.For example the implementation of the routing infrastructure in Web API is entirely different from MVC and we as developers have to struggle to learn two different ways to do the same thing.
There is a duplication of functionality in the framework classes as the types for the frameworks are implemented in different assemblies .The consequence is there are separate updates and new releases of the above frameworks even for the same functionality like routing or model binding.
They seem to fit together well, sharing lot of common concepts rather then being segregated into different frameworks.
MVC 6
This is what MVC 6 is. It merges the three frameworks into a single framework. MVC 6 consists of all these frameworks. Though it is called MVC 6 but it consists of all the three frameworks. So it consists of the next versions of MVC5 , WebAPI 2 and WebPages2.
We can use our existing knowledge of these frameworks in MVC 6 as well but as the underlying core ASP.NET framework has changed so there are many new concepts that we may need to use in MVC 6.
One of the big changes in ASP.NET 5 is that it doesn’t run on top of the old ASP.NET pipeline that consists of HTTPHandlers and HTTPModules .It runs on top of a new composable pipeline in which we can add middleware components.
New features in MVC 6
Can be run other hosts then IIS
While MVC5 can be hosted in IIS and runs on top of asp.net pipeline ,MVC 6 can be self hosted and uses flexible pipeline in which we have complete control over the components that are part of the pipeline.
Cloud optimized
Since MVC 6 is part of the ASP.NET 5 ,which 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.
Environment based configuration system
MVC 6 includes new environment based configuration system,unlike depending on just the web.config file as in the previous versions.Deploying MVC 5 applications to the cloud requires many configuration changes.MVC 6 applications can be very easily deployed to the cloud because of the environment based configuration system.Our application works with the configuration providers which retreives the value from the different configuration sources like XML file.
Dependency injection
In MVC 6 dependency injection is supported across all the technologies , WebAPI,MVC and WebPages.A default dependency injection container is provided out of the box which provides minimal functionality and is useful when we require only limited functionality.We can very easily add our own dependency injection container by implementing the IServiceProvider interface.This is the interface that is implemented by the deafult container as well ,so we can replace the default implementation with our own conatiner.
Supports OWIN
MVC 6 supports the OWIN abstraction.So MVC 6 applications consists of composable pipeline in which we have complete control over the various components in the pipeline.
Important components of an MVC 6 Application
When we create a new ASP.NET 5 application few files are added to the solution explorer.
As these are new files in MVC 6 application let’s have a look into what each of these files contains.
Config.json This file contains the application configuration information. We can define our application configuration in different places not just this file. Using the configuration value providers the correct configuration values are picked. So our application is not concerned how to connect to different sources to get the configuration values.
Below we have added a connection string in the config.json file.
Hide Copy Code
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=server_name;Database=database_name;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
Project.json This file contains the project dependencies as well as the build information. This can also contain the commands used by the application.
Hide Copy Code
{
"webroot" : "wwwroot",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-alpha4",
"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 By default the host looks for a class called Startup with a Configure method that takes IApplicationBuilder as a parameter.
Global.json defines the location for project references so that projects can reference each other.
NuGet Packages If we right click the ASP.NET reference node ,there is no option to add the assembly reference.It is because the unit of reference in MVC 6 application is nuget package and not assembly unlike the previous version.
Other nice feature of nuget references is that we don't need to specify all the dependent nuget packages instead they are referenced by default.In the solution explorer we can see all the dependencies for the packages nad we do not need to specify all these packages ,rather we just specify the main nuget package and the rest are added automatically by the framework.
Defining the Request Pipeline
If we look in the startup.cs file it contains a Configure method
Hide Copy Code
public void Configure(IApplicationBuilder app) { // Setup configuration sources
var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();
IApplicationBuilder is the parameter which is passed by the host when the application starts. The above code creates an object of configuration class.We are adding the conjig.json and Environment variables as the source of configuration values.Also we can add other configuration sources that our application requires.
Our application can fetch configuration values from different configuration sources as there are configuration value providers for different sources .So our application don't need to be concerned about the different sources of configuration.This is one reason that MVC 6 applications can be easily moved to cloud.
In Configure method we need to add the services used by our application to the services container. UseServices is an extension method which we use to add the services we require in our application.We need to pass a parameter of type servicecollection to which we add all the required services for our application.
For example to add the service for EntityFramework we use AddEntityFramework() extension method defined for the IServiceCollection interface.
Hide Copy Code
app.UseServices(services =>
{
// Add EF services to the services container
services.AddEntityFramework()
.AddSqlServer();
In MVC 6 we need to explicitly add the middleware components to the request pipeline unlike the previous versions which have a well defined pipeline.We add the components using the UseXXX extension methods.If we want to add the middleware for MVC then we add it using the UseMvc extension method.
Hide Copy Code
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
The routing component is defined in the Microsoft.ASPNET.Routing class which is common to Web API and MVC.This is unlike previous versions in which routing was included in System.Web.Routing namespace.
Like rest of the components we setup in the request pipeline ,we add routes as a middleware component.In the above route we have made the id as optional atribute by adding the ? after id.
Creating a controller
If we look at the controller it is similar to the controller in MVC 5 application.
Hide Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
namespace HelloASP.NET.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
Though there are few differences .
Namesapce for MVC 6 is Microsoft.AspNet.Mvc unlike System.Web.Mvc in the previous versions.
One other big difference between the controller in MVC 5 application and MVC6 is that a controller in MVC 6 application does not need to derive from the Controller class.Though most of the time we may need to derive from the controller class as it provides lots of default functionality available to our controller.But if we don’t need access to all the functionality provided by the controller class then we can define our controller class as a normal class or POCO.
So if we change our HomeController to a normal class it is perfectly valid in MVC 6
Hide Copy Code
public class HomeController
{
public string Index()
{
return "hello mvc 6";
}
}
If we execute the above action method we may see the below page.Our normal c# class ,without being inherited from the Controller class , is working as a controller .
The controller class is defined in the Microsoft.AspNet.Mvc namespace unlike System.Web.Mvc in the previous versions.
ASP.NET vNext Overview & features (MVC6, Web API, Web Pages and Single R)
Microsoft has announced to launch ASP.NET vNext, which includes MVC, Web API, and Web Pages frameworks will be merged into one framework, called MVC 6. This new framework removes a lot of overlap between the existing MVC and Web API frameworks. It uses a common set of abstractions for routing, action selection, filters, model binding, and so on. You can use the framework to create both UI (HTML) and web APIs.
ASP.NET vNext has been designed to provide you the facilities to create .NET stack for building modern cloud-based apps. ASP.NET vNext will build on .NET vNext.
What’s included in ASP.NET vNext:
- Web Forms, MVC 5, Web API 2, Web Pages 3, SignalR 2, EF 6, Identity 2 will be fully supported on .NET vNext.
- MVC, Web API, Web Pages 6, SignalR 3, EF 7, Identity 3
- MVC, Web API and Web Pages have been merged into a single framework MVC 6. For example, there’s now unified controller and routing concepts between all three.
- New project system
- New configuration system
- No System.Web, new lightweight HttpContext (not System.Net.Http)
- We will have a migration tool which will help you migrate your application to use ASP.NET vNext on .NET vNext and cloud optimized .NET vNext. This will cover scenarios such as migrating from MVC 5 to 6 and more.
Following are the main features of Asp.NET vNext:
- ASP.NET vNext Provides Cloud-OptimizedThis feature is really very helpful for developers. ASP.NET vNext can target .NET vNext (with cloud optimized mode). This means that services such as session state and caching can be replaced based on whether the app is running in the cloud or in a traditional hosting environment.
- Version of .NET Framework Side by side supportWhen targeting the Cloud optimized mode in .NET vNext, ASP.NET vNext will let you deploy any other version of .NET Framework. Since now the .NET vNext Framework can be deployed with the app, each app can run different versions of .NET vNext side-by-side and upgrade separately, all on the same machine.
- More Enhancement for developerIn ASP.NET vNext, you can now edit your code files and refresh the browser to see the changes without explicitly building your app. You can also edit your application outside of Visual Studio.ASP.NET vNext projects have project.json file where all the project dependencies are stored. This makes it easier to open vNext projects outside of Visual Studio so that you can edit them using any editor such as Notepad etc. You can even edit ASP.NET vNext projects in the cloud.
- Helps in building Web sites and services bothMVC and Web API have been merged into a single programming model. For example, there’s now unified controller, routing and model binding concepts between them. You can now have a single controller that returns both MVC views and formatted Web API responses, on the same HTTP verb.
- Modular StackASP.NET vNext will ship as NuGet packages. NuGet packages will also be the unit of reference in your application. NuGet packages and libraries references will be treated the same so it will be easier to manage the references in your project.This makes it possible for an application developer to choose what functionality they want to bring into their application. In the previous versions of ASP.NET features such as HttpContext, Session, Caching, and Membership were baked into the framework. As an app developer now if you do not need these features then you can choose not to bring it into your app.
- Support for Dependency InjectionDependency Injection is built into vNext and is consistent across the stack. All of the vNext components such as MVC, Web API, SignalR, EF and Identity will use the same DI. This will allow us to provide the right set of services based on the environment that you are running in.
- New ConfigurationThere is a new configuration system which can read values from environment variables. This configuration provides a unified API system for accessing configuration values. You can use the same APIs to access configuration values locally or in Azure.
- vNext is Open SourceThe entire source code is already released as open source via the .NET Foundation. You can see the source at https://github.com/aspnet and follow progress on vNext in real time. You can also send pull requests and contribute to the source code.
- vNext have Cross-platform supportWe're developing vNext with cross-platform in mind, including an active collaboration with Xamarin to ensure that cloud-optimized .NET applications can run on Mac or Linux on top of the Mono runtime.
Reference: http://blogs.msdn.com/b/webdev/archive/2014/06/03/asp-net-vnext-in-visual-studio-14-ctp.aspx
Summary: In this article, we have learn some overview and features about ASP.NET vNext. I hope you would like this article. If you have any query regarding same, Please post your comments below.
No comments:
Post a Comment