我有一個 .net cor 3.1 Blazor 服務器專案。我在這個專案中使用默認的身份系統。我想在不登錄的情況下訪問特定的剃須刀頁面。我怎么做?任何想法?
我嘗試添加@attribute [AllowAnonymous]到剃刀頁面。但這對我不起作用
_Host.cshtml
page "/"
@using Microsoft.AspNetCore.Authorization
@namespace has.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@attribute [Authorize]
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>has</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="css/custom_styles.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material Icons" rel="stylesheet">
<link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet" />
<link href="_content/Blazor.ContextMenu/blazorContextMenu.min.css" rel="stylesheet" />
<script src="_content/BlazorInputFile/inputfile.js"></script>
<script src="~/js/site.js"></script>
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">??</a>
</div>
<script src="_framework/blazor.server.js"></script>
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
<script src="_content/Blazor.ContextMenu/blazorContextMenu.min.js"></script>
<script src="https://kit.fontawesome.com/863157cf0e.js" crossorigin="anonymous"></script>
</body>
</html>
App.razor
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<Error404 />
</LayoutView>
</NotFound>
</Router>
<AntContainer />
</CascadingAuthenticationState>
uj5u.com熱心網友回復:
我找到了答案。@attribute [AllowAnonymous]我在檔案夾內的一個單獨檔案夾中創建了另一個 _Host.cshtml,Pages并將我的剃須刀頁面放在該檔案夾中。
添加@layout PublicLayout到您的匿名訪問組件。在我的例子中,MyAnonymousComponent.razor。PublicLayout是PublicSection/PublicLayout.razor
我的專案結構是這樣的
MyProject
-Pages
-Section1
-Section2
-Shared
_Layout.cshtml
-PublicSection // This folder has anonymous access components
_Host.cshtml
_Layout.cshtml
MyAnonymousComponent.razor
PublicApp.razor
PublicLayout.razor
_Host.cshtml
-Shared
MainLayout.razor
App.razor
_Imports.razor
Program.cs
Startup.cs
PublicSection/_Host.cshtml
@page "/publicsection"
@using has.Pages.PublicRazor
@using Microsoft.AspNetCore.Authorization
@namespace has.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@attribute [AllowAnonymous]
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<base href="~/PublicSection" />
// rest is same as Original _Host.cshtml file
</head>
<body>
<app>
<component type="typeof(PublicApp)" render-mode="ServerPrerendered" />
</app>
// rest is same as Original _Host.cshtml file
</body>
</html>
PublicApp.cshtml
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(PublicLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(PublicLayout)">
<Error404 />
</LayoutView>
</NotFound>
</Router>
<AntContainer />
</CascadingAuthenticationState>
PublicApp.razor
@inherits LayoutComponentBase;
<Content Class="site-layout-background content-pan">
@Body
</Content>
@code {
}
啟動.cs
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("~/PublicSection/{**segment}","/PublicRazor/_Host");
endpoints.MapFallbackToPage("/_Host");
});
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/443167.html
標籤:C# asp.net 核心 asp.net 身份 西装外套 blazor 服务器端
下一篇:試圖理解kotlin目標代碼塊
