ASP. NET Core Web API minimizes project and coreapi

Source: Internet
Author: User

ASP. NET Core Web API minimizes project and coreapi

The default ASP. NET Core template in ASP. NET Core contains a Web API template to create a Web API project.

Sometimes, you only need to create an API and do not need to care about Razor, localization or XML serialization. By deleting useless NuGet software packages and code, you can increase the API loading time and reduce the deployment package size.

 

Create a project

Open VS2017 and create an ASP. NET Core application (. NET Core) project named miniwebapi. After confirming, select the Web API template and set "authentication" to "no authentication ".

Then confirm that the project is created. The content of the csproj file of the default project is as follows:

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

 

Delete A NuGet package

Delete Microsoft. AspNetCore. Mvc.

Microsoft. VisualStudio. Web. CodeGeneration. Tools and Microsoft. ApplicationInsights. AspNetCore can also be deleted.

Add

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

The final miniwebapi. csproj file is as follows:

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

In fact, Microsoft. Extensions. Logging. Debug can be deleted if it is not needed. Here we make a reservation.

Configuration Service

If Microsoft. ApplicationInsights. AspNetCore is removed, remove. UseApplicationInsights () from Program. cs ()

Open the Startup. cs file and remove services. AddMvc () in the ConfigureServices method ();

Then change it to the following:

services.AddMvcCore().AddJsonFormatters();

 

Then change the default ValuesController. cs to the following:

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

The focus is to remove the default inherited Controller.

If you have other requirements such as cross-origin and data verification, you can add the corresponding NuGet package.

Microsoft. AspNetCore. Mvc. Cors the cross-domain correspondence in services. AddMvcCore (). AddJsonFormatters (). AddCors ();

Microsoft. AspNetCore. Mvc. DataAnnotations data validation attribute.AddDataAnnotations();

 

Test

Run the program and use the debugging function. VS2017 will automatically open the browser and access the corresponding api/values, as shown below:

 

Indicates that the interface can be accessed successfully.

In this way, you can only use the required functions to reduce the loading time. ASP. NET Core allows you to flexibly use what you want.

 

If you think this article is helpful to you, click"Recommendation", Thank you.

 

Related Article

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.