基於 Blazui 的 Blazor 后台管理模板 Blazui.Admin 正式嘗鮮


簡介

  Blazui.Admin 是一個基於Blazui的后台管理模板,無JS,無TS,非 Silverlight,非 WebForm,一個標簽即可使用。
  我將在下一篇文章討論 Blazor 服務器端渲染與客戶端渲染的基本原理,對比服務器端渲染與 WebForm 的異同點
  經過近一個月的開發,Blazui.Admin 嘗鮮版終於搞定了,功能很有限,同時也存在很多問題,只集成了一個后台管理系統最基本的功能,包括:

  • 選項卡式頁面管理,無 Iframe
  • 二級導航菜單
  • Identity 用戶注冊與登錄,基於Cookies

  需要注意的一點是我們短時間不會支持 IdentityServer4 以及Jwt,但會在接下來的計划中支持 Session 注冊與登錄。下面是 BlazAdmin 的運行效果

初次運行時會創建管理員

image.png-40.7kB

主界面

image.png-81.7kB

修改密碼

image.png-84.2kB

登出

image.png-82.3kB

馬上開始嘗鮮

准備條件

  • .net core 3.1
  • VS2019

新建一個 Blazor 服務端渲染應用

image.png-49.6kB

安裝 Blazui.Admin.ServerRender Nuget 包,注意圖片中的包名是錯的

image.png-160.2kB

刪除 NavMenu.razor 文件

image.png-73.6kB

_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 類

image.png-22.6kB

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() 方法之下
image.png-32.2kB

增加 WebApi 相關配置,這主要為登錄服務

image.png-27.6kB

_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

image.png-123.3kB

運行查看效果

image.png-44.2kB

Demo 下載

示例 Demo 獲取請進QQ群 74522853

Fuck Fork Me, Star Me


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM