我一直在 Blazor WASM 中创建一个项目并使用 Identity Server
一切正常,解决方案正在构建中。但是在某个阶段,我必须更改身份服务器上的设置,因为这现在会阻止未登录的用户注册或访问登录页面。
尝试导航到 https://localhost:44351/authentication/register 我在开发人员工具中收到以下控制台消息
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed. These requirements were not met:
DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
Run Code Online (Sandbox Code Playgroud)
据我所知,一切都相当标准
登录部分
@using Microsoft.AspNetCore.Identity
@using MDIS.SAW.Server.Models
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
var returnUrl = "/";
if (Context.Request.Query.TryGetValue("returnUrl", out var existingUrl)) {
returnUrl = existingUrl;
}
}
<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/" method="post">
<button type="submit" …Run Code Online (Sandbox Code Playgroud) 我在 .NET Core 3.1 中创建了一个 Blazor WebAssembly 托管模板。然后右键单击project.Client/wwwroot/css文件夹并单击Add client side library。然后选择 Font Awesome 库并安装它。我将以 下行添加到index.html<head>。
<link href="css/font-awesome/css/fontawesome.css" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)
我有libman.json的:
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"library": "font-awesome@5.11.2",
"destination": "wwwroot/css/font-awesome/"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我仅将以下行添加到默认 Blazor 模板页面Counter.razor(Razor 组件)。IntelliSense 找到字体:
@page "/counter"
<h1>Counter</h1>
<span class="fa fa-save"></span>
@code {}
Run Code Online (Sandbox Code Playgroud)
但我只看到一个正方形:
我正在尝试了解 Blazor 的行为方式。我正在调试一些东西,但暂时删除注释其他代码,以免被破坏并确保我所观察到的内容。代码如下所示。
<OneComponent @ref="_oneComponent" param1="@varParam1" param2="@varParam2"></OneComponent>
Run Code Online (Sandbox Code Playgroud)
@code {
private OneComponent _oneComponent;
private _objectOne varParam1; // There are values here.
private _objectTwo varParam2; // There are values here as well.
private async Task SaveClicked()
{
if (_oneComponent.OnSaveClicked())
{
// nothing here.
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,我注意到OnParametersSetAsync()正在 <OneComponent/>重新运行。我的问题是,即使我没有更改 varParam1、varParam2 中的任何一个,为什么会OnParametersSetAsync()再次重新运行?是这样吗?当程序现在指向_oneComponent返回 true 或 false 后的该组件时,是否应该重新运行?
我今天通过以下命令行安装了 Blazor WebAssembly 示例项目 (WeatherForecast):
dotnet new blazorwasm --hosted -o ProjectName
Run Code Online (Sandbox Code Playgroud)
我在调试模式下通过 Visual Studio 2019 安装后直接启动了 SPA。浏览器是:Chrome。浏览器显示应用程序,但是当我选择导航菜单“获取数据”时,什么也没有发生。Chrome 显示 1 个错误:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'.
System.NotSupportedException: The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'.
at System.Net.Http.Json.HttpContentJsonExtensions.ValidateContent (System.Net.Http.HttpContent content) <0x2e87f38 + 0x0009a> in <filename unknown>:0
at System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync[T] (System.Net.Http.HttpContent content, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x2e87d30 + …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-web-api blazor blazor-client-side blazor-webassembly
我正在尝试 Microsoft 的待办事项列表示例:https ://docs.microsoft.com/en-us/aspnet/core/tutorials/build-a-blazor-app?view=aspnetcore- 3.1
我想添加一个待办事项,而不是用鼠标点击按下按钮,我想按下回车键。我对在这个解决方案中使用 JS 不满意:How to set the focus to an InputText element? 我尝试通过这行代码触发方法 private void Enter(KeyboardEventArgs e):
<button @onclick="AddTodo" @onkeypress="@(e=>Enter(e)" tabindex="0" >Add todo</button>
Run Code Online (Sandbox Code Playgroud)
它没有用。
enter code here
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo" @onkeypress="Enter" tabindex="0" >Add todo</button>
@code {
private IList<TodoItem> todos = new List<TodoItem>();
private string newTodo;
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
//private void Enter(KeyboardEventArgs e)
private void …Run Code Online (Sandbox Code Playgroud) 我读了这篇文章,但仍然遇到问题:
我有我的 Blazor WebAssembly 站点,其中有一个静态 index.html 文件,可以以正常方式引导 Blazor。在其中<head>,我有<link href="css/app.min.css" rel="stylesheet" />。该文件是使用部署时的 Dockerfile RUN 步骤从 SCSS 构建的。
根据该问题,我不需要缓存清除 URL,因为 Blazor 会根据需要自动请求一个新的 URL。然而,当我部署站点并在 Firefox 中打开它时,我得到了旧样式,并在 Firefox 开发人员工具网络选项卡中看到 app.min.css 是从磁盘缓存中检索的。我必须按 Ctrl+F5 才能获取新样式。
那么这个链条到底出了什么问题呢?在我看来,每次部署时我仍然需要 CSS 的缓存清除 URL。 需要进行哪些设置才能在站点二进制文件更改时下载我的 CSS(按照 Steve Sanderson 在该问题中建议的方式)?
我的目标是能够从配置文件更改环境名称。如果服务器配置为X,那么客户端也应该使用X环境。
你知道,在 ASP.NET 中我们可以使用launchSettings.json将 IIS 服务器配置为开发,并将“真实”服务器配置为发布到生产。WASM 客户端应该看到相同的配置。我想只在这里配置一下环境,以减少发布服务器时忘记东西的后果。
有多篇文章描述了 Blazor 环境,但我对正确设置它感到困惑。
为了让客户端看到环境,我将这一行添加到 Client/Program.cs 文件中:
Console.WriteLine("Using environment " + builder.HostEnvironment.Environment);
Run Code Online (Sandbox Code Playgroud)
实验:
如果我将 Server/launchSettings.json/profiles/IIS Express/environmentVariables/ASPNETCORE_ENVIRONMENT 从开发更改为生产,则该网站根本无法加载(返回 404)。
在其他一些情况下,ASPNETCORE_ENVIRONMENT客户端只是忽略/无法访问。
在本地运行应用程序时,环境默认为开发。发布应用程序时,环境默认为“生产”。
对我来说有时确实如此,但并非总是如此。在某些情况下,即使我在本地运行,环境也是生产环境。
Blazor.start({ environment: "Staging" });本文中描述的没有效果。另外,这需要一个字符串,我如何传递服务器端配置变量的值?
使用 web.config,我总是可以覆盖环境名称。有两个问题:
blazor-environment控制环境名称的标头。为什么?
删除 web.config
在 Server/Startup.cs 中添加:
app.Use(async (context, next) =>
{
context.Response.Headers.Add("blazor-environment", env.EnvironmentName);
await next.Invoke();
});
Run Code Online (Sandbox Code Playgroud)
在Chrome DevTools中,我可以看到标头的值确实是Development。但客户打印的是 Production。
如果标头被忽略,为什么我要在 web.config 中设置它?它已设置,但 clinet WASM 项目无法读取标头,它怎么知道该名称?引用的 Blazor WASM JavaScript 文件是否已更改,环境名称是否被编译到其中?
还有生成的Client/launchSettings.json,似乎完全被忽略了。
至少描述了 10 种获取环境名称的方法。有些使用 appsettings.json (单独下载,或 …
对于 Blazor WebAssembly,我提出了使用 SQLite 的想法。这个问题提到这是不可能的。是否可以在 Blazor WebAssembly 中使用 SQLite?如果可以,如何使用?
我有两个元素,一个输入文件和一个按钮。我想在单击按钮时模拟单击 InputFile。我现在可以与 Js 一起使用,但想知道是否有 blazor 的方法可以做到这一点。 一个 gif 示例
这是当前的 blazor 代码
<InputFile id="filePicker" />
<button type="button" @onclick="OpenFilePicker">or Select</button>
private async Task OpenFilePicker() => await JsRuntime.InvokeVoidAsync("clickElement");
Run Code Online (Sandbox Code Playgroud)
通过这个 Js 代码我可以让它工作,
clickElement = () => document.getElementById('filePicker').click();
Run Code Online (Sandbox Code Playgroud)
有没有更好的不依赖Js的解决方案?
今天我有一个问题,这是其中一个问题,你一直想知道但你总是害怕问;)但是这个问题也与 BlazorWebAssembly 应用程序 docker 支持相关。所以也许我会描述我做了什么。
所以我确实想玩一点尖端 blazor,当然还有 .NET 5.0(我的最后一个“尖端技术”是 ASP.NET MVC - 所以是的,已经有一段时间了:))
一开始我不太清楚那些与 Blazor 相关的选项是做什么用的。所以我选择了:
您可以发现,“启用 Docker 支持”选项是灰色的。所以就像“docker支持问题预览”:)
我不知道什么选项:“ASP.NET Core 托管”代表,所以我没有使用它(这是一个错误)一旦您选中此选项,它将创建一个服务器应用程序,一个客户端(Blazor)一个和一个共享(这是为了他们之间的模型目的)
我不知道,所以我必须添加我自己的 WebApi 项目和模型的一些项目:)
现在,下一步是添加 docker 支持(令人惊讶的是这是可能的)
这创建了一个 docker 文件:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["BlazorApp/BlazorApp.csproj", "BlazorApp/"]
COPY ["../SearchMovies.Model/SearchMovies.Model.csproj", "../SearchMovies.Model/"]
RUN dotnet restore "BlazorApp/BlazorApp.csproj"
COPY . .
WORKDIR "/src/BlazorApp"
RUN …Run Code Online (Sandbox Code Playgroud) blazor ×8
asp.net-core ×3
c# ×3
.net ×1
.net-5 ×1
asp.net ×1
css ×1
docker ×1
environment ×1
font-awesome ×1
razor ×1
sqlite ×1