用户可选的属性路由以指示 dotnet 核心 web 中的租户 api
User optional Attribute Route to indicate tenant in dotnet core web api
有没有办法为 REST dotnet 核心 webapi 项目定义可选路由参数?
我想在我的 API 控制器中定义一个可选的 tenant
参数。如果参数存在,我想用它来过滤我的查询。
api.example.com/tenant1/users
-> tenant1的所有用户
api.example.com/users
-> 系统所有用户
[Route ("{tenant:guid?}/[controller]")]
public class UsersController {
// GET api.example.com/{tenant}/users
[HttpGet]
public async Task<IActionResult> GetUsers (Guid tenant) {
...
if(tentant != null){
var users = await _db.Users.GetAllByTentant(tenant);
return Ok (users);
} else {
var users = await _db.Users.GetAll();
return Ok (users);
}
}
}
感谢玉里的暗示
[Route ("[controller]")]
public class UsersController {
// GET api.example.com/users
[HttpGet]
public async Task<IActionResult> GetUsers () {
...
var users = await _db.Users.GetAll();
return Ok (users);
}
// GET api.example.com/{tenant}/users
[HttpGet("/{tenant}/[controller]")]
public async Task<IActionResult> GetUsers (Guid tenant) {
...
var users = await _db.Users.GetAllByTentant(tenant);
return Ok (users);
}
}
有没有办法为 REST dotnet 核心 webapi 项目定义可选路由参数?
我想在我的 API 控制器中定义一个可选的 tenant
参数。如果参数存在,我想用它来过滤我的查询。
api.example.com/tenant1/users
-> tenant1的所有用户
api.example.com/users
-> 系统所有用户
[Route ("{tenant:guid?}/[controller]")]
public class UsersController {
// GET api.example.com/{tenant}/users
[HttpGet]
public async Task<IActionResult> GetUsers (Guid tenant) {
...
if(tentant != null){
var users = await _db.Users.GetAllByTentant(tenant);
return Ok (users);
} else {
var users = await _db.Users.GetAll();
return Ok (users);
}
}
}
感谢玉里的暗示
[Route ("[controller]")]
public class UsersController {
// GET api.example.com/users
[HttpGet]
public async Task<IActionResult> GetUsers () {
...
var users = await _db.Users.GetAll();
return Ok (users);
}
// GET api.example.com/{tenant}/users
[HttpGet("/{tenant}/[controller]")]
public async Task<IActionResult> GetUsers (Guid tenant) {
...
var users = await _db.Users.GetAllByTentant(tenant);
return Ok (users);
}
}