. Net Core (iii) MVC core

Source: Internet
Author: User

The MVC core changes feel pretty big, the features needed are mostly installed from NuGet, the IOC is built in, the Selfhost is supported, and so on.

I. Changes in the structure of the project
The structure of the new MVC project created has changed, for example: Static files need to be uniformly placed into the Wwwroot folder, The config file became Appsettings.json, replaced the global file with the Startup.cs file, and the previous reference to the view namespace was configured in Web. config under view, and now it's changed to use _ viewimport.cshtml files, and is configured with razor syntax.

Ii. built-in IOC
A) AUTOFAC is a good IOC container, and it can still be used under. Net core, and the core also has a built-in ioc,typescript that comes with IOC, perhaps injected as a function of the language itself.
The built-in IOC also supports the selection of multiple object lifecycles, such as transient (the internal request is instantiated), Scoped (per-Web request), Singleton (the entire app lifecycle).
b) The injection of a single object is only set in Startup.configureservices: Services.addsingleton (typeof (person));
The constructor is then injected in the place where the person is used.
c) The actual application of a single object can not be injected, according to the assembly to configure automatic injection method is:
var serviceasm = Assembly.Load (New AssemblyName ("2_1 MVC Core Lib"));
var servicetypes = Serviceasm.gettypes (). Where (t = typeof (Iservicetag)
. IsAssignableFrom (t) &&!t.gettypeinfo (). IsAbstract);
foreach (Var servicetype in servicetypes) {
var interfacetypes = Servicetype.getinterfaces (). Where (t = = t.name! = "Iservicetag");
foreach (Var interfacetype in interfacetypes) {
Services. Addsingleton (InterfaceType, servicetype);
}
}
Add T. The restriction of Name! = "Iservicetag" is necessary, Iservicetag is the token of the service interface, and if this judgment is not added, Iservicetag will also be injected.

d) IOC can be injected "recursively", but ensure that the root object is also created by the IOC, not explicit new. For example, if an IOC is used to create a exceptionfilter, the other classes it references can be automatically injected. The methods for manually creating Nfilter with IOC and adding them to MVC are:
Services. ADDMVC (options =
var serviceprovider = Services. Buildserviceprovider ();
var filter = serviceprovider.getservice<exceptionfilter> ();
Options. Filters.add (filter);
});

In front-end code, you can also use HttpContext.RequestServices.GetService to let IOC create objects.


Learning materials: such as Peng Net. NET Accelerated http://www.rupeng.com/News/10/4603.shtml

. Net Core (iii) MVC core

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.