图文说明,注意流量.数据库
建好以后就像下面这样
json
继续再创建两个.net core类库项目分别是 ApiStudy.Core
和 ApiStudy.Infrastructure
api
一个解决方案下三个项目:浏览器
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=398940 public 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(); //使用默认路由 } } }
用来向容器中注册服务,注册好的服务能够在其余地方进行调用.markdown
用来配置中间件管道,即如何响应http请求.
app
代码以下:网站
namespace ApiStudy.Api.Controllers { using Microsoft.AspNetCore.Mvc; [Route("api/[controller]")] [ApiController] public class UserController:Controller { public IActionResult Get() { return Ok("Hello"); } } }
修改lauchSetting.json以下:ui
{ "profiles": { "ApiStudy.api": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
浏览器访问 https://localhost:5001/api/user
this