标签:调用 config cep startup 连接 seh images result launch
图文说明,注意流量.
建好之后就像下面这样
继续再建立两个.net core类库项目分别是 ApiStudy.Core
和 ApiStudy.Infrastructure
一个解决方案下三个项目:
namespace ApiStudy.api
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public IConfiguration Configuration { get; }
public StartupDevelopment(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
注意, services.AddControllers();
和 endpoints.MapControllers();
方法成对出现, 这是.net core3.0中的最新写法
用来向容器中注册服务,注册好的服务可以在其他地方进行调用.
用来配置中间件管道,即如何响应http请求.
将 Startup
类修改为 StartupDevelopment
新建 StartupProduction
类
StartupProduction
namespace MyBlog
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public class StartupProduction
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseHttpsRedirection();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
修改 Program类的代码
配置使用的 Startup 类
webBuilder.UseStartup(Assembly.GetExecutingAssembly().GetName().FullName);
netcore会根据这里配置的环境是来选择使用哪个Startup类
Development -> StartupDevelopment
Production -> StartupProduction
代码如下:
namespace ApiStudy.Api.Controllers
{
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController]
public class UserController:Controller
{
public IActionResult Get()
{
return Ok("Hello");
}
}
}
修改lauchSetting.json, 删除iis有关的部分, 如下:
{
"profiles": {
"ApiStudy.api": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
浏览器访问 https://localhost:5001/api/user
Asp.NetCoreWebApi入门 - 从零开始新建api项目
标签:调用 config cep startup 连接 seh images result launch
原文地址:https://www.cnblogs.com/Laggage/p/11105937.html