ASP.NET Core Web API 最小化項目


ASP.NET Core中默認的ASP.NET Core 模板中有Web API 模板可以創建Web API項目。

有時,只需要創建一個API,不需要關心Razor,本地化或XML序列化。通過刪除無用的NuGet軟件包和代碼,可以提高 API 的加載時間並減少部署包大小。

 

新建項目

打開VS2017 新建一個ASP.NET Core 應用程序 (.NET Core)項目,命名為miniwebapi。確定后選擇Web API 模板,並將“身份驗證”設置為“不進行身份驗證”。

然后確定就創建好了項目,默認項目的csproj 文件內容如下:

<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>

 

刪除NuGet包

首先刪除掉  Microsoft.AspNetCore.Mvc。

其實  Microsoft.VisualStudio.Web.CodeGeneration.Tools 及也可以刪除 Microsoft.ApplicationInsights.AspNetCore 。

接着添加

  • Microsoft.AspNetCore.Mvc.Core
  • Microsoft.AspNetCore.Mvc.Formatters.Json

最終miniwebapi.csproj文件如下:

<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>

 其實Microsoft.Extensions.Logging.Debug  如果不需要也可以刪除,這里做了一個保留。

配置服務

對於移除了Microsoft.ApplicationInsights.AspNetCore 的,需要在Program.cs 中去掉.UseApplicationInsights()

接着打開Startup.cs 文件,在ConfigureServices 方法中去掉 services.AddMvc();

然后改成如下:

services.AddMvcCore().AddJsonFormatters();

 

接着打開默認的ValuesController.cs 更改成如下:

    [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)
        {
        }
    }

重點是去掉默認的繼承 Controller。

如果你有其他的需求如跨域,數據驗證,可以再添加對應的NuGet包。

Microsoft.AspNetCore.Mvc.Cors 跨域 對應的在services.AddMvcCore().AddJsonFormatters().AddCors();

Microsoft.AspNetCore.Mvc.DataAnnotations 數據驗證屬性。AddDataAnnotations();

 

測試

運行程序,使用調試功能,VS2017 會自動打開瀏覽器並訪問對應的api/values,顯示如下:

 

表示接口能夠成功訪問。

這樣你可以只使用所需的功能,從而減少加載時間。ASP.NET Core 可以讓你靈活的使用想要使用的。

 

如果你覺得本文對你有幫助,請點擊“推薦”,謝謝。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM