ASP. NET Core MVC now provides a true "one ASP." Framework that can is used for building both APIs and websites. But what if the want to build API?
Most of the ASP. NET Core MVC Tutorials I ' ve seen advise using the package Microsoft.AspNetCore.Mvc . While the this does indeed give you the need to build APIs, it also gives your a lot more:
- Microsoft.AspNetCore.Mvc.ApiExplorer
- Microsoft.AspNetCore.Mvc.Cors
- Microsoft.AspNetCore.Mvc.DataAnnotations
- Microsoft.AspNetCore.Mvc.Formatters.Json
- Microsoft.AspNetCore.Mvc.Localization
- Microsoft.AspNetCore.Mvc.Razor
- Microsoft.AspNetCore.Mvc.TagHelpers
- Microsoft.AspNetCore.Mvc.ViewFeatures
- Microsoft.Extensions.Caching.Memory
- Microsoft.Extensions.DependencyInjection
- Netstandard.library
A Few of these packages is still needed if you ' re building APIs but many is specific to building full websites.
After installing the above package we typically register MVC in like so Startup.ConfigureServices :
services.AddMvc();
This code was responsible for wiring the necessary MVC services with application container. Let's look at what this actually does:
public static imvcbuilder ADDMVC ( Span class= "Hljs-keyword" >this iservicecollection services) {var builder = Services. Addmvccore (); Builder. Addapiexplorer (); Builder. Addauthorization (); Adddefaultframeworkparts (builder. Partmanager); //order added affects options Setup order //Default Framework Order Builder. Addformattermappings (); Builder. Addviews (); Builder. Addrazorviewengine (); Builder. Addcachetaghelper (); //+1 order Builder. Adddataannotations (); //+1 order //+10 order Builder. Addjsonformatters (); Builder. Addcors (); return new mvcbuilder (builder. Services, builder. Partmanager)}
Again Most of the service registration refers to the components used for rendering Web pages.
Bare Metal APIs
It turns out that the ASP. Anticipated that developers could only want to build APIs and nothing else, so they gave us the ability to does just that.
First of all, rather than installing Microsoft.AspNetCore.Mvc , only install Microsoft.AspNetCore.Mvc.Core . This would give you the bare MVC middleware (routing, controllers, HTTP results) and is a lot else.
In order to process JSON requests and return JSON responses we also need the package Microsoft.AspNetCore.Mvc.Formatters.Json .
Then, to add both the core MVC middleware and JSON formatter, add the following code to ConfigureServices :
public void ConfigureServices(IServiceCollection services){ services.AddMvcCore() .AddJsonFormatters();}
The final thing to do are to change your controllers to derive from ControllerBase instead of Controller . This provides a base class for MVC controllers without any View support.
Looking at the final list of packages in Project.json, you can see we really don ' t need that much after all, especially GI Ven most of these is related to configuration and logging:
"Microsoft.AspNetCore.Mvc.Core":"1.1.0","Microsoft.AspNetCore.Mvc.Formatters.Json":"1.1.0","Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", "Microsoft.AspNetCore.Server.Kestrel": " 1.1.0 ", " Microsoft.Extensions.Configuration.EnvironmentVariables ": "1.1.0", "Microsoft.Extensions.Configuration.FileExtensions": Span class= "hljs-string" > "1.1.0", "Microsoft.Extensions.Configuration.Json": "1.1.0", "Microsoft.Extensions.Configuration.CommandLine": " 1.1.0 ", " Microsoft.Extensions.Logging ": 1.1.0 ", " Microsoft.Extensions.Logging.Console ": " 1.1.0 ", "Microsoft.Extensions.Logging.Debug": "1.1.0"
You can find the complete code on GITHUB.
Original: Http://benfoster.io/blog/bare-metal-apis-with-aspnet-core-mvc
Bare metal APIs with ASP. NET Core MVC (RPM)