Asp.NetCoreWebApi入门 - 从零开始新建api项目
生活随笔
收集整理的這篇文章主要介紹了
Asp.NetCoreWebApi入门 - 从零开始新建api项目
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 開發(fā)環(huán)境
- 打開VS,建立項(xiàng)目
- 項(xiàng)目模板
- 修改StartUp類代碼
- ConfigureServices方法
- Configure方法
- 新建一個(gè)Controller
- F5運(yùn)行
- 完
圖文說明,注意流量.
開發(fā)環(huán)境
- Visual Studio 2019
- .net core 2.x
打開VS,建立項(xiàng)目
建好之后就像下面這樣
繼續(xù)再建立兩個(gè).net core類庫項(xiàng)目分別是 ApiStudy.Core 和 ApiStudy.Infrastructure
- 右擊解決方案,新建項(xiàng)目.
- 選擇 .NetCore類庫項(xiàng)目.
- 輸入項(xiàng)目名.
- ApiStudy.Core項(xiàng)目建立完成
- 同樣的方法再建立ApiStudy.Infrastructrue 項(xiàng)目.
- 完成之后如圖
- 然后設(shè)置依賴關(guān)系
項(xiàng)目模板
一個(gè)解決方案下三個(gè)項(xiàng)目:
- Xxxx.Core
放一些核心的東西,比如 Entity(實(shí)體) 類 - Xxxx.Infrastructure
放一些數(shù)據(jù)庫連接之類(DbContext)的 - Xxxx.Api
網(wǎng)站項(xiàng)目
修改StartUp類代碼
namespace ApiStudy.api {using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.DependencyInjection;public class Startup{// This method gets called by the runtime. Use this method to add services to the container.// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940public void ConfigureServices(IServiceCollection services){services.AddMvc();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseMvc(); //使用默認(rèn)路由}} }ConfigureServices方法
用來向容器中注冊服務(wù),注冊好的服務(wù)可以在其他地方進(jìn)行調(diào)用.
Configure方法
用來配置中間件管道,即如何響應(yīng)http請求.
新建一個(gè)Controller
代碼如下:
修改lauchSetting.json如下:
{"profiles": {"ApiStudy.api": {"commandName": "Project","launchBrowser": true,"applicationUrl": "https://localhost:5001;http://localhost:5000","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"}}} }F5運(yùn)行
瀏覽器訪問 https://localhost:5001/api/user
完
轉(zhuǎn)載于:https://www.cnblogs.com/Laggage/p/11105937.html
總結(jié)
以上是生活随笔為你收集整理的Asp.NetCoreWebApi入门 - 从零开始新建api项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如今的移动操作系统,写在2013年——a
- 下一篇: ASP.NET MVC3源码下载