Those are my notes related to pluralsight course. If you want the course, just go on pluralsight.com and see the entire course.
Project structure
It is here a major change as now, the project is based on file structure. So, if you want to add a new file, just add it in the correct spot of your folder structure and it will be taken automatically.
The file system is your project and everything in, builds dynamically.
This is happening because of the new Roslyn compiler which compiles in memory. There are no temporary asp.net folder, because of this.
Because of that, also the project structure is dramatically changed. The reasons for that big change:
- it is easier to deploy an application with the new structure, by cloning a repository
- it is also because of the optimization for cloud
- it is also because of an unification MVC6 = ASP.NET MVC + WEB API.
There is a new CLR for this ASP.NET 5 and it is wrapped in NuGet packages. It is significantly small and faster. It is also working on Linux.
The ASP.NET applications no longer requires System.Web.dll. Because of this, the earning is the context carried with each request is now 2KB. With the old version, at least 30KB.
In ASP.NET 5, basic concepts are the same: controllers, views, css and javascript, etc.
But also there are new concepts that were changed:
1. The root of the website is no longer the root of the project.
2. The root of the website is called now wwwroot.
- this because it is now a clear separation between files and information sent to client and configuration and logic of the application.
- this will contain the static assets (css, images, js, etc).
- it is containing also the "bin" folder.
The project.json configuration file it is telling to asp.net where to find the root folder and other configurations (like dependecies, etc).
It is json format.
Now the dependencies are related to NuGet management based on project.json
There are few reasons for this change:
1. Dependency management
2. ASP.NET is moving away from requiring Windows or VS, etc.
Where packages are stored
ASP.NET 5 can work on multiple frameworks. The most common ones are ASP.NET 5 (for windows) and ASP.NET Core 5 (for the rest). Of course, those frameworks are specified in project.json config file, so the project will be compiled under both frameworks.
Because the application can be built on multiple platforms, not all platforms will expose same methods. So, in this situation you can use compiler conditionals like:
The version of ASP.NET 5 was initially named "Project K". For this reason you can find a lot of Ks. Depends what you framework you pick, you could run the application against ASP.NET 5 or CORE.
#ifdef ASPNET50 // do something#endif#ifdef ASPNETCORE50 // do something else#endif
The packages are stored now under User's directory, in a folder: .kre (K (the project) - R = runtime, E = environment). Global packages will stay in the same place, but in .kpm folder.
ASP.NET 5 loves NuGet packages.
Also, in project.json, where you define what frameworks you would like to use, you can specify for each one a list of specific dependencies. So let all common dependencies in the "dependecies" tag, and the specific ones on "aspnet50" or "aspnetcore50" or so on.
If you want to add as a reference a project from your solution, you should do this in dependencies tab (from project.json) or in each framework tab (if the project is not supported on the current consumable project).
Build an ASP.NET 5 application from scratch
There are some new changes in the new ASP.NET 5:
1. Global.asax file was replaced by Startup.cs
2. Web.config file was replaced by same file, Startup.cs
In ASP.NET 5 is a new http pipeline.
Also ASP.NET 5 tries to find a class called Startup in the root of the application and it is passing an IApplicationBuilder object. In this ASP.NET 5 Linux, this is how the application is configured.
Adding ASP.NET MVC 6, first you need to go in project.json and add there, in dependencies, Microsoft.ASP.Net.MVC (version 6).
When you add a reference, usually there is a method that is starting with Use... for each package that you added. You need to use this method in order to activate it.
Because ASP.NET MVC 6 is in the end a service for application and because it is used dependency injection for that, we need to make some changes in couple of places:
1. Startup.cs -> update/add ConfigureServices(IServiceCollection services). This IServiceCollection is a dependency injection reference. This method will take place before of application configuration (Configure method). In this way we can have services ready to go by the time of configuring the application. For example, to use Mvc, you need first to add services.UseMvc()
2. Startup.cs -> update/add Configure (IApplicationBuilder app) with app.UseMvc().
Routing - in order to perform routing specifications, there are a couple of ways to do that:
1. In Startup.Configure, when use UseMvc() there is an overload that takes an expression lambda like:
app.UseMvc (routes => routes.MapRoute("default",
"{controller=Home}/{action=Index}");
- spre deosebire de MVC anterior, valorile default se baga dupa controller, action, etc.
2. Using attributes (which was introduced starting with MVC 5), but there are some features refined a little bit in vNext.
Using attributes you are able to decorate the Controller class with
- [Route("/Home"] or [Route("/")] - last one in order to make it default.
- There is an update: [Route("/[controller]")] in order to take the name of the class.
- In order to define the default action, you may decorate the action with [Route("[action]")] and/or [Route("/")].
Views - are very similar with the old MVC.
Working around controllers is usually managed by a code called Service. Service is available through controllers. In order to have a service, we need to have (generally speaking) followings:
a. A class that is transporting the message or information - this is the model
b. An interface of the server (IService...) which is implementing my methods.
c. The concrete implementation of that class (that is implementing also the IService... interface, in order to add it through DependencyInjection.
- The dependecy injector can be easily used in constructor. In the previous versions of ASP.NET, it was not possible. But now, in order to use it, you need to go in ConfigureServices and set it there, like:
i) service.AddInstance<xxx> - equivalent with AddSingleton
ii) service.AddScope - a new service for each http request
iii) service.AddTransient - a new instance when someone needs one
Configuration
Of an ASP.NET 5 was changed. There are no xml files. You can do anything via config.json which should be added in the root of the site.
If the application is connected to a database, then it requires to read some text from config.json.
In order to do this, you need to add the text that you want in config.json, like:
"messageName" : "text of the message what you want"
Also, you need a dependency in order to read this information from there: Microsoft.Framework.ConfigurationModel.Json.
Usually, you need to add an IConfiguration Configuration {get ; set; } in order to keep your configuration options locally.
And you need to initialize it, in the constructor of Startup:
Configuration = new Configuration().AddJsonFile("config.json")
After that, in the constructor of the service that you are using, just need to add IConfiguration interface. The DI will give you a reference to it. After you have it, you can call it like: _config.Get("message");
You can use here also AddEnvironmentsVariables
WebAPI
ASP.NET 5 MVC includes also WebAPI.
WebAPI controllers are derived from the same controllers as the usual MVC Controllers. The difference is WebAPI is returning only data, the MVC controllers are returning a view.
Tools in ASP.NET 5
KVM - key version manager
It is allowing you to play with ASP.NET 5 versions, to install, update, etc by command line.
It installs also the k utility (which is an ASP.NET 5 utility). It is running very well if you are in the path where project.json is located.
In order to work, you need to add in project.json a new section called "commands" and all commands from there can be executed by k.
NodeJs and ES6
NodeJs is installed default with VS2015
- it is a simple javascript execution runtime, based on same engine used by Google in Chrome to execute javascript.
- it can run on server
Bower
- it allows you to install and keep last version of packages that you are using (jquery, etc).
- its focus is on front end development (comparing with npm's focus what is the nodejs server)
There are also some build events that can be specified.
Grunt
- it allows you to minify, compile, unit testing and so on. Repetitive tasks.
- it requires NodeJs
No comments:
Post a Comment