New Project
Open VS2017 to create a new ASP. NET Core Application project, named Miniwebapi. When you are sure, select the Web API template and set authentication to no authentication.
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/443844/201707/443844-20170705162533237-556954074. PNG "style=" margin:0px;padding:0px;border:0px; "/>
Then make sure to create the project, the default project's csproj file content is as follows:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
<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>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
Delete a NuGet package
First Remove MICROSOFT.ASPNETCORE.MVC.
In fact Microsoft.VisualStudio.Web.CodeGeneration.Tools and can also delete Microsoft.ApplicationInsights.AspNetCore.
Then add
The final miniwebapi.csproj file is as follows:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
<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>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
In fact Microsoft.Extensions.Logging.Debug if not need can also delete, here made a reservation.
Configure the service
For removal of Microsoft.ApplicationInsights.AspNetCore, it needs to be removed from the Program.cs. Useapplicationinsights ()
Then open the Startup.cs file and remove services from the Configureservices method. Addmvc ();
Then change to the following:
Services. Addmvccore (). Addjsonformatters ();
Then open the default ValuesController.cs change to the following:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
[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) { } }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
The point is to remove the default inherited Controller.
If you have other requirements such as cross-domain, data validation, you can add the corresponding NuGet package again.
Microsoft.AspNetCore.Mvc.Cors the cross-domain corresponding in services. Addmvccore (). Addjsonformatters (). Addcors ();
Microsoft.AspNetCore.Mvc.DataAnnotations data validation Properties.AddDataAnnotations();
Test
Run the program, using the debugging function, VS2017 will automatically open the browser and access the corresponding api/values, shown as follows:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/443844/201707/443844-20170705170337565-673056144. PNG "style=" margin:0px;padding:0px;border:0px; "/>
Indicates that the interface can be successfully accessed.
This allows you to use only the features you need to reduce the load time. The ASP. NET Core gives you the flexibility to use what you want to use.
ASP. NET Core Web API minimized project