我正在开发一个应用程序,我需要为网络和移动设备添加多个集合。但招摇却是空的。这是我的代码
namespace RIOnlySelfCareService.Controllers
{
[Route("api/web/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly IAccountService accountService;
private readonly IMenuService _menuService;
public AccountController(IAccountService accountService, IMenuService menuService)
{
this.accountService = accountService;
_menuService = menuService;
}
[HttpPost("getMenus")]
public async Task<IActionResult> GetMenusAsync()
{
return Ok(await _menuService.GetMenusAsync());
}
}
}
namespace RIOnlySelfCareService.Controllers.MobileControllers
{
[Route("api/mobile/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly IAccountService accountService;
private readonly IMenuService _menuService;
public AccountController(IAccountService accountService, IMenuService menuService)
{
this.accountService = accountService;
_menuService = menuService;
}
[HttpPost("getMenus")]
public async Task<IActionResult> GetMenusAsync()
{
return Ok(await _menuService.GetMenusAsync());
}
}
}
程序.cs
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("web", new OpenApiInfo
{
Title = "Self Care Web API",
Version = "v1"
});
c.SwaggerDoc("mobile", new OpenApiInfo
{
Title = "Self Care Mobile API",
Version = "v1"
});
c.DocInclusionPredicate((docName, apiDesc) =>
{
// Check if the controller's route contains the specified prefix
if (apiDesc.TryGetMethodInfo(out MethodInfo methodInfo))
{
var controllerAttribute = methodInfo.DeclaringType?.GetCustomAttributes(true)
.OfType<RouteAttribute>()
.FirstOrDefault();
if (controllerAttribute != null)
{
var routePrefix = controllerAttribute.Template.ToLower();
// Group controllers based on route prefixes
if (docName == "web" && routePrefix.StartsWith("api/web"))
return true;
if (docName == "mobile" && routePrefix.StartsWith("api/mobile"))
return true;
}
}
return false;
});
}
if (!app.Environment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
// Render Swagger UI for web controllers
options.SwaggerEndpoint("/swagger/web/swagger.json", "Self Care Web API");
// Render Swagger UI for mobile controllers
options.SwaggerEndpoint("/swagger/mobile/swagger.json", "Self Care Mobile API");
});
}
将这行代码拆分为
methodInfo.DeclaringType?.GetCustomAttributes(true).OfType<RouteAttribute>().FirstOrDefault();
,如下所示:
var CustomAttributes = methodInfo.DeclaringType?.GetCustomAttributes(true);
var controllerAttributeOfType = CustomAttributes.OfType<RouteAttribute>();
我发现使用
OfType
后,controllerAttributeOfType
一直为空,if (controllerAttribute != null)
条件下的代码无法执行。
所以灵魂是,我们需要使用
Microsoft.AspNetCore.Mvc.RouteAttribute
,那么问题就会得到解决。
这是我的测试代码和测试结果:
c.DocInclusionPredicate((docName, apiDesc) =>
{
var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
if (apiDesc.TryGetMethodInfo(out MethodInfo methodInfo))
{
var CustomAttributes = methodInfo.DeclaringType?.GetCustomAttributes(true);
var controllerAttributeOfType = CustomAttributes.OfType<Microsoft.AspNetCore.Mvc.RouteAttribute>();
var controllerAttribute = methodInfo.DeclaringType?.GetCustomAttributes(true)
.OfType<Microsoft.AspNetCore.Mvc.RouteAttribute>()
.FirstOrDefault();
if (controllerAttribute != null)
{
var routePrefix = controllerAttribute.Template.ToLower();
var include = (docName == "web" && routePrefix.StartsWith("api/web")) ||
(docName == "mobile" && routePrefix.StartsWith("api/mobile"));
logger.LogInformation($"API {methodInfo.DeclaringType?.Name}.{methodInfo.Name} ({routePrefix}) inclusion for {docName}: {include}");
return include;
}
}