1. Visual Studio 选择插件
2. 新建项目
3. 新建 Service 类库(右击解决方案,新增),再新建DService类
4. 将Service 引用到 Controller(右键demo1->添加->项目引用)
5. 将DService注入到全局
6. Controller内引用 并 封装api接口
using Demo1Service;
using Microsoft.AspNetCore.Mvc;
namespace demo1.Controllers
{
[ApiController]
[Route("index")]
public class IndexController : ControllerBase
{
private readonly DService1 dService1;
public IndexController(DService1 _dService1)
{
this.dService1 = _dService1;
}
[HttpGet]
[Route("GetA")]
public String GetA()
{
var a = dService1.GetA();
return a;
}
}
}
运行结果