ASP.NET Core中默認(rèn)的ASP.NET Core 模板中有Web API 模板可以創(chuàng)建Web API項(xiàng)目。
陽(yáng)谷網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),陽(yáng)谷網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為陽(yáng)谷上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的陽(yáng)谷做網(wǎng)站的公司定做!
有時(shí),只需要?jiǎng)?chuàng)建一個(gè)API,不需要關(guān)心Razor,本地化或XML序列化。通過(guò)刪除無(wú)用的NuGet軟件包和代碼,可以提高 API 的加載時(shí)間并減少部署包大小。
打開(kāi)VS2017 新建一個(gè)ASP.NET Core 應(yīng)用程序 (.NET Core)項(xiàng)目,命名為miniwebapi。確定后選擇Web API 模板,并將“身份驗(yàn)證”設(shè)置為“不進(jìn)行身份驗(yàn)證”。

然后確定就創(chuàng)建好了項(xiàng)目,默認(rèn)項(xiàng)目的csproj 文件內(nèi)容如下:
.gif)
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include="wwwroot\" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" /> </ItemGroup></Project>
.gif)
首先刪除掉 Microsoft.AspNetCore.Mvc。
其實(shí) Microsoft.VisualStudio.Web.CodeGeneration.Tools 及也可以刪除 Microsoft.ApplicationInsights.AspNetCore 。
接著添加
Microsoft.AspNetCore.Mvc.Core
Microsoft.AspNetCore.Mvc.Formatters.Json
最終miniwebapi.csproj文件如下:
.gif)
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include="wwwroot\" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.3" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="1.1.3" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /> </ItemGroup></Project>
.gif)
其實(shí)Microsoft.Extensions.Logging.Debug 如果不需要也可以刪除,這里做了一個(gè)保留。
對(duì)于移除了Microsoft.ApplicationInsights.AspNetCore 的,需要在Program.cs 中去掉.UseApplicationInsights()
接著打開(kāi)Startup.cs 文件,在ConfigureServices 方法中去掉 services.AddMvc();
然后改成如下:
services.AddMvcCore().AddJsonFormatters();
接著打開(kāi)默認(rèn)的ValuesController.cs 更改成如下:
.gif)
[Route("api/[controller]")] public class ValuesController
{ // GET api/values [HttpGet] public IEnumerable<string> Get()
{ return new string[] { "linezero", "linezero's blog" };
} // GET api/values/5
[HttpGet("{id}")] public string Get(int id)
{ return "linezero"+id;
} // POST api/values [HttpPost] public void Post([FromBody]string value)
{
} // PUT api/values/5
[HttpPut("{id}")] public void Put(int id, [FromBody]string value)
{
} // DELETE api/values/5
[HttpDelete("{id}")] public void Delete(int id)
{
}
}.gif)
重點(diǎn)是去掉默認(rèn)的繼承 Controller。
如果你有其他的需求如跨域,數(shù)據(jù)驗(yàn)證,可以再添加對(duì)應(yīng)的NuGet包。
Microsoft.AspNetCore.Mvc.Cors 跨域 對(duì)應(yīng)的在services.AddMvcCore().AddJsonFormatters().AddCors();
Microsoft.AspNetCore.Mvc.DataAnnotations 數(shù)據(jù)驗(yàn)證屬性。AddDataAnnotations();
運(yùn)行程序,使用調(diào)試功能,VS2017 會(huì)自動(dòng)打開(kāi)瀏覽器并訪問(wèn)對(duì)應(yīng)的api/values,顯示如下:

網(wǎng)站欄目:ASP.NETCoreWebAPI最小化項(xiàng)目
轉(zhuǎn)載來(lái)于:http://www.yijiale78.com/article8/jdoiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、響應(yīng)式網(wǎng)站、微信公眾號(hào)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)