Extend a business module
On this page
Add a device API, menu, button permission and page route to the standalone sample host. The example API returns a fixed device code.
Choose stable identifiers
| Identifier | Example | Consumers |
|---|---|---|
| Module key | devices | Backend module catalog, menu Module, frontend request header |
| Menu code | devices_list | Seed identity and menu uniqueness |
| Page path | /devices/list | Menu, trusted path resolver and Vue Router |
| Component key | devices.list | Backend route validation and frontend component registry |
| API permission | devices:read | [Permission], button seed and frontend permission checks |
| HTTP endpoint | POST /api/devices/List | RPC store and host request adapter |
Module keys, paths, component keys and permissions serve separate purposes. Display names and menu database IDs do not replace them.
Declare a protected API
Add DeviceApi.cs to the host:
using TAdmin;
using TouchSocket.Http;
using TouchSocket.WebApi;
/// <summary>通过宿主已有认证及授权管线公开设备读取能力。</summary>
[Router("/api/devices/[action]")]
public sealed class DeviceApi : IPluginApiServer
{
/// <summary>返回固定设备编码,实际项目在此调用带管理域条件的业务服务。</summary>
[Permission("devices:read")]
[WebApi(Method = HttpMethodType.Post)]
public string[] List() => ["P-001"];
}In the host’s RegisterControllers(RpcStore store), register the API before publishing the permission catalog:
store.RegisterServer<DeviceApi>();
AdminApiPermissionCatalog.SetRpcStore(store);Keep ValidationFilter, AuthorizationFilter, UnifyResultFilter and OperateLogFilter on that same store. Attributes alone do not register an endpoint or install authorization.
Supply module, menu and button seeds
Add DeviceModuleProvider.cs. Seed IDs must be unique within your catalog, and retain the same business identity across releases.
using TAdmin;
/// <summary>声明设备模块拥有的导航和权限;此提供器不持有会话或请求状态。</summary>
public sealed class DeviceModuleProvider : IAdminModuleProvider, IAdminSeedDataProvider, IMenuRouteProvider
{
/// <summary>设备菜单的稳定种子身份,不随展示名称变化。</summary>
private const long MenuId = 920001;
/// <summary>向后端可信目录声明模块及其同源前端根路径。</summary>
public AdminModuleDefinition GetModule() => new()
{
Key = "devices", Name = "设备", EnglishName = "Devices", FrontendPath = "/devices"
};
/// <summary>账号继续由宿主现有引导流程管理。</summary>
public IEnumerable<User> GetUsers() => [];
/// <summary>角色继续由现有管理服务维护。</summary>
public IEnumerable<Role> GetRoles() => [];
/// <summary>将设备页加入本模块的可授权菜单。</summary>
public IEnumerable<Menu> GetMenus() =>
[
new Menu
{
Id = MenuId, MenuCode = "devices_list", MenuName = "设备列表",
Module = "devices", Path = "/devices/list", ComponentKey = "devices.list",
MenuType = MenuType.Menu, ParentId = 0, Sort = 10,
IsEnabled = true, Visible = true, IsSystem = true
}
];
/// <summary>将菜单翻译与稳定菜单身份关联。</summary>
public IEnumerable<MenuLocale> GetMenuLocales() =>
[
new MenuLocale { Id = 9200010, MenuId = MenuId, Locale = "zh-CN", LocalizedName = "设备列表" },
new MenuLocale { Id = 9200011, MenuId = MenuId, Locale = "en-US", LocalizedName = "Devices" }
];
/// <summary>使角色授权页面能分配与 API 完全一致的读取权限码。</summary>
public IEnumerable<Button> GetButtons() =>
[
new Button { Id = 9200010, MenuId = MenuId, ButtonName = "查询", ButtonCode = "devices:read", Sort = 10 }
];
/// <summary>仅接受本提供器拥有的真实路径,未知路径留给其他提供器解析。</summary>
public string? ResolveComponentKey(string? path) =>
string.Equals(path, "/devices/list", StringComparison.Ordinal) ? "devices.list" : null;
}Add these registrations to SampleStartup.ConfigureServices:
services.AddSingleton<IAdminModuleProvider, DeviceModuleProvider>();
services.AddSingleton<IAdminSeedDataProvider, DeviceModuleProvider>();
services.AddSingleton<IMenuRouteProvider, DeviceModuleProvider>();The provider is stateless, so each interface can use a separate instance.
Connect the frontend
Explicitly register /devices/list and map devices.list to a Vue page supplied by your host. Import components from trusted code. Database seeds do not generate a page. Follow the sample’s web/src/router and the frontend adapter contract.
For login/menu snapshots of this module, the host client sends X-Admin-Module: devices. This selects navigation from the backend’s registered catalog; it is not a tenant authorization credential. Business requests retain the host’s Bearer session, realm and cancellation handling.
Use hasPermission('devices:read') for the page’s read action. The backend authorizes the same code through [Permission]. Refresh authentication and menu snapshots after role permissions change.
Verify the complete extension
- Start the host and confirm
DeviceApiis in the RPC store anddevices:readappears in the role permission catalog. - With an authorized role, check the menu, the actual
/devices/listpage and its API call. - Call the API directly as a signed-in role without this permission and verify rejection by the authorization pipeline. Hiding a button is insufficient.
- Attempt to save an unknown component key or a path owned by another module and verify trusted-route validation rejects it.
Real queries must also apply the current realm and data scope. Source examples: TAdmin/samples/TAdmin.Sample/Permission/SampleMenuProvider.cs and SampleStartup.cs. See permissions and host extensions for the authorization boundary.