Bare metal APIs with ASP.NET Core MVC(转)
ASP.NET Core MVC now provides a true "one asp.net" framework that can be used for building both APIs and websites. But what if you only want to build an API?
Most of the ASP.NET Core MVC tutorials I've seen advise using the Microsoft.AspNetCore.Mvc
package. While this does indeed give you what you need to build APIs, it also gives you 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 are still needed if you're building APIs but many are specific to building full websites.
After installing the above package we typically register MVC in Startup.ConfigureServices
like so:
services.AddMvc();
This code is responsible for wiring up the necessary MVC services with application container. Let's look at what this actually does:
public static IMvcBuilder AddMvc(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.NET team anticipated that developers may only want to build APIs and nothing else, so they gave us the ability to do just that.
First of all, rather than installing Microsoft.AspNetCore.Mvc
, only install Microsoft.AspNetCore.Mvc.Core
. This will give you the bare MVC middleware (routing, controllers, HTTP results) and not a lot else.
In order to process JSON requests and return JSON responses we also need the Microsoft.AspNetCore.Mvc.Formatters.Json
package.
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 is 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 given most of these are 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": "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.
原文:http://benfoster.io/blog/bare-metal-apis-with-aspnet-core-mvc
Bare metal APIs with ASP.NET Core MVC(转)的更多相关文章
- ASP.NET Core 中文文档 第四章 MVC(01)ASP.NET Core MVC 概览
原文:Overview of ASP.NET Core MVC 作者:Steve Smith 翻译:张海龙(jiechen) 校对:高嵩 ASP.NET Core MVC 是使用模型-视图-控制器(M ...
- 【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expo ...
- [转]【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
本文转自:https://www.cnblogs.com/inday/p/6288707.html HTTP is not just for serving up web pages. It’s al ...
- 剖析ASP.NET Core MVC(Part 1)- AddMvcCore(译)
原文:https://www.stevejgordon.co.uk/asp-net-core-mvc-anatomy-addmvccore发布于:2017年3月环境:ASP.NET Core 1.1 ...
- ASP.NET Core MVC/WebAPi 模型绑定探索
前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...
- ASP.NET Core MVC 配置全局路由前缀
前言 大家好,今天给大家介绍一个 ASP.NET Core MVC 的一个新特性,给全局路由添加统一前缀.严格说其实不算是新特性,不过是Core MVC特有的. 应用背景 不知道大家在做 Web Ap ...
- ASP.NET Core MVC 中的 [Controller] 和 [NonController]
前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
- ASP.NET Core 中文文档 第二章 指南(4.1)ASP.NET Core MVC 与 Visual Studio 入门
原文:Getting started with ASP.NET Core MVC and Visual Studio 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:刘怡(Alex ...
随机推荐
- vue搭建项目
vue-cli 作用:快速搭建项目脚手架 安装3.0:npm i -g @vue/cli 安装桥接工具:npm i -g @vue/cli-init (vue-cli 3和旧版使用相同的命令,所以2被 ...
- 微信小程序真机预览跟本地不同的问题。原文地址:https://blog.csdn.net/qq_27187991/article/details/69664247/
微信小程序中出现最多的一个问题,就是真机跟本地不同:我简单列举一些我发现的原因,给大家参考,大家也可以把自己发现的东西回复给我,给我参考: 本地看不到数据,就先让本地能看到数据,再看本帖....特别提 ...
- linux c makefile
unio : unio.c gcc unio.c -o unio run: ./unio 上面有错.必须强制按照规则来: 目标体:依赖文件 命令 命令必在目标体的下一行,且要加TAB键,必须必须. ...
- 虚拟机安装与Linux命令的学习 ——20155215宣言
一.虚拟机的安装 虚拟机的安装对我来说真可谓是一波三折.打开老师发布的安装教程,简单地浏览了一下,主要步骤都有图文解说.我本来以为这个安装按部就班即可,可哪知道,问题一个接着一个出现. 问题1 在我下 ...
- 2017-2018-1 20155319《信息安全技术》实验二——Windows口令破解
2017-2018-1 20155319<信息安全技术>实验二--Windows口令破解 实验环境 试验系统:Windows 2003 实验工具:LC5.SuperDic 实验原理 一.口 ...
- #20155327 2016-2017-2 《Java程序设计》第三周学习总结
20155327 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 一.三种重要的数字表示 无符号:编码基于传统的二进制表示法,表示大于或者等于零的数字. 补码 ...
- 20155328 《Java程序设计》实验三 敏捷开发与XP实践 实验报告
一.编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性. 下面是没有最基本的缩进的一个程序: publi ...
- [agc002D]Stamp Rally-[并查集+整体二分]
Description 题目大意:给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的边的最大编号最小.n, ...
- 苏州Uber优步司机奖励政策(4月11日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- day2 HTML - body
<body>内常用标签 1.基本标签 所有标签分为: # 块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标, ...