簡介
Blazui.Admin 是一個基於Blazui的后台管理模板,無JS,無TS,非 Silverlight,非 WebForm,一個標簽即可使用。
我將在下一篇文章討論 Blazor 服務器端渲染與客戶端渲染的基本原理,對比服務器端渲染與 WebForm 的異同點
經過近一個月的開發,Blazui.Admin 嘗鮮版終於搞定了,功能很有限,同時也存在很多問題,只集成了一個后台管理系統最基本的功能,包括:
- 選項卡式頁面管理,無 Iframe
- 二級導航菜單
- Identity 用戶注冊與登錄,基於Cookies
需要注意的一點是我們短時間不會支持 IdentityServer4 以及Jwt,但會在接下來的計划中支持 Session 注冊與登錄。下面是 BlazAdmin 的運行效果
初次運行時會創建管理員
主界面
修改密碼
登出
馬上開始嘗鮮
准備條件
- .net core 3.1
- VS2019
新建一個 Blazor 服務端渲染應用
安裝 Blazui.Admin.ServerRender Nuget 包,注意圖片中的包名是錯的
刪除 NavMenu.razor 文件
_Imports.razor 文件增加以下內容
@using Blazui.Admin
@using Blazui.Component
為了啟用登錄,App.razor 文件的內容需要替換為如下
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
登錄需要用到數據庫,所以添加 DemoDbContext 類
public class DemoDbContext : IdentityDbContext
{
public DemoDbContext(DbContextOptions options) : base(options)
{
}
}
缺少什么命名空間就直接 using,不再贅述
Startup 文件 ConfigureService 方法替換為如下內容
示例為了方便所以用到了內存數據庫,需要安裝 nuget 包 Microsoft.EntityFrameworkCore.InMemory
需要 using 相關的命名空間
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DocsDbContext>(options =>
{
options.UseInMemoryDatabase("docs");
});
services.AddScoped<DbContext, DocsDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddHttpClient();
await services.AddBlazuiServicesAsync();
services.AddSingleton<RouteService>();
services.AddScoped<IUserService, TUserService>();
services.AddAdmin<DocsDbContext>();
}
Startup 文件 Configure 方法增加如下內容
增加登錄相關配置
app.UseAuthorization();
app.UseAuthentication();
注意需要加到 app.UseRouting() 方法之下
增加 WebApi 相關配置,這主要為登錄服務
_Host.cshtml 頁面內容替換如下
@page "/"
@namespace BlazorApp4.Pages //此處 BlazorApp4 需要改成你實際的命名空間,一般就是項目名
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazAdmin Demo</title>
<base href="~/" />
<link href="/_content/BlazAdmin/css/admin.css" rel="stylesheet" />
<link rel="stylesheet" href="/_content/Blazui.Component/css/index.css" />
<link rel="stylesheet" href="/_content/Blazui.Component/css/fix.css" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="/_content/Blazui.Component/js/dom.js"></script>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
接下來就是測試菜單和頁面,將 MainLayout.razor 文件的內容替換為如下
@inherits LayoutComponentBase
<BAdmin Menus="Menus" NavigationTitle="BlazAdmin Demo" EnablePermissionMenus="false"></BAdmin>
@code{
protected List<MenuModel> Menus { get; set; } = new List<MenuModel>();
protected override void OnInitialized()
{
Menus.Add(new MenuModel()
{
Label = "示例頁面",
Icon = "el-icon-s-promotion",
Children = new List<MenuModel>() {
new MenuModel(){
Label="示例子頁面1",
Icon = "el-icon-s-promotion",
Route="/page1"
},
new MenuModel(){
Label="示例子頁面2",
Icon = "el-icon-s-promotion",
Route="/page2"
}
}
});
}
}
在 Pages 頁面下新建兩個 Razor 組件,注意是 Razor 組件,將路由分別設置為 /page1 和 /page2
運行查看效果
Demo 下載
示例 Demo 獲取請進QQ群 74522853
Fuck Fork Me, Star Me
- Blazui 系列組件庫:https://github.com/wzxinchen/Blazui