The next version of ASP.NET is ASP.NET 5 which has been redesigned from the ground up. ASP.NET 5 supports the concepts that are expected in any modern web application like deploying on cloud and loosely coupled applications. Some of the new features in ASP.NET 5 are
- HTTP pipeline which we can totally configure to add the components we require
- Self hosted applications
- Unification of different technologies MVC ,WebAPI and Web Pages in a common framework
- Cross platform .ASP.NET applications can now run on platforms like Linux and Mac
- Open source.ASP.NET 5 is open source and is available on GitHub.
We can create a ASP.NET 5 web application in visual studio 2015.
To create a new web apppication we select a new project option in visual studio ,a new project dialog box is displayed. Select the Web Application template and click next.
We select the Emty template in the MVC 6 project dialog and click next.We can uncheck the host in cloud option below as we will be running this application locally.
Once the new project is created we see that few files have been added by default in the soultion.These are new file types in ASP.NET 5.
Some of the new files that are introdced in ASP.NET 5 are
- package.json Contains the packages required by our application.When the application is installed the dependencies listed are also installed.
- project.json Contains the configuration values in the form of key value pairs
- Startup.cs Code to configure the request pipeline.Also contains the project startup code.
In the startup class the two important methods that we need to configure application are
Use the following method to Add the Add the services required by our application such as MVC
public void ConfigureServices(IServiceCollection services) { // To add MVC 6 service use services.AddMvc(); }
Use the following method to Add the components to the request pipeline
public void Configure(IApplicationBuilder app) { //To add MVC to the pipeline add app.UseMvc(); }
If we look at the test controller it is defined as
[Route("api/[controller]")] public class TestController : Controller
There are few important things to note in the above code
We have a single controller class for both MVC and webapi applications instead of having a separate api controller for webapi as in previous versions of WebAPI.
We use the Route attribute to map the URL with the WebAPI controller class.So the above controller will handle the request for URL’s such as api/Test .api/Test will handle the GetAll request.
Leave a Reply