我有一个Blazor带有客户端 Razor 页面的项目。在该页面上,我想显示多行文本,例如:
未找到系列
错误消息
但是文本是在 C# 字符串中使用@bind.
我试过使用法线\n来换行。它确实选择了它是命令,但它在文本中放置了一个“空格”而不是换行。
我也试过<br />在文中写,但后来它只是在文中写了。
我的剃须刀页面:
<p>@resultString</p>
@code {
string resultString = "Series not found \nError message";
}
Run Code Online (Sandbox Code Playgroud)
使用代码片段中的输入,我希望得到以下输出:
未找到系列
错误消息
我正在尝试学习 ASP.NET Blazor 中的新功能。我正在使用 Visual Studio 2019。我正在尝试创建一个想法注册表。所以我从 Bootstrap 4 中获取了下拉列表的代码。它没有按预期工作。你能告诉我我哪里做错了吗?
只是有点不知所措,任何建议将不胜感激。
给定代码:
<!-- Card Body -->
<div class="card-body">
<!-- <form -->
<form>
<div class="form-group">
<label for="exampleFormControlInput1">Title</label>
<input type="email" class="form-control" id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="4"></textarea>
</div>
<!-- Basic dropdown -->
<div class="form-group">
<button class="btn btn-primary dropdown-toggle mr-4" type="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Basic dropdown
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">.Net</a>
<a class="dropdown-item" href="#">Python</a>
<a class="dropdown-item" href="#">Data Science</a>
<div class="dropdown-divider"></div>
</div>
</div>
<!-- Basic dropdown -->
Run Code Online (Sandbox Code Playgroud) 我注意到*.wasmcompiler by的文件大小Rust是可以接受的。然而,一个最小的 HelloWorld 编译器AspNet/Blazor将占用近 2.8MB。
mono.wasm 1.75MB
mscorlib.dll 1.64MB
*.dll ....
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,那mono.wasm就是在浏览器中运行并运行我们编写的 dll 的 VM。这是否意味着无论我们做什么,我们都不能使文件的大小小于 1.75MB?如果没有,有没有办法减小文件大小?
在 WebAPI .net 核心项目中,我创建了一个验证 api 密钥的中间件类。通过验证它,它检索密钥在 invoke 方法中拥有的权限(用户或管理员)。
我通过一个开关来设置原理
GenericIdentity identity = new GenericIdentity("API");
GenericPrincipal principle = null;
//we have a valid api key, so set the role permissions of the key
switch (keyValidatorRes.Role)
{
case Roles.User:
principle = new GenericPrincipal(identity, new[] { "User" });
context.User = principle;
break;
case Roles.Admin:
principle = new GenericPrincipal(identity, new[] { "Admin" });
context.User = principle;
break;
default:
principle = new GenericPrincipal(identity, new[] { "Other" });
context.User = principle;
break;
}
Run Code Online (Sandbox Code Playgroud)
在控制器方法上我有
[Authorize(Roles = …
我正在尝试使用IHostedService创建后台服务.如果我只有一个后台服务,一切正常.当我尝试创建多个实现时,IHostedService只有第一个实际运行的实现.
services.AddSingleton<IHostedService, HostedServiceOne>();
services.AddSingleton<IHostedService, HostedServiceTwo>();
Run Code Online (Sandbox Code Playgroud)
在上面的例子StartAsync上HostedServiceOne被调用,但StartAsync上HostedServiceTwo不会被调用.如果我换登记的两种实现方式的顺序IHostedService(把IHostedServiceTwo前IHostedServiceOne),然后StartAsync在HostedServiceTwo被调用而从来不是HostedServiceOne.
编辑:
我被引导到以下内容:
然而,这不适合IHostedService.要使用建议的方法,我将不得不打电话,serviceProvider.GetServices<IService>();但似乎IHostedService.StartAsync似乎在内部调用.我甚至不确定我会在哪里触发它IHostedService.StartAsync.
我正在基于最新的 Core 3.1 开发 Blazor 项目。
UI 文化正确显示日期和数字,如图所示。
但是当我使用 EditForm 时,数字和日期没有按照它应该的格式进行格式化。
所以这个 EditForm 的代码部分
<InputDate id="date" class="form-control" @bind-Value="@TaskObject.Date" />
Run Code Online (Sandbox Code Playgroud)
所以在 EditForm 中它看起来像这样,这不是正确的文化格式:
但是在 UI 中看起来像这样,这没关系:
当我刚接触 Blazor 时,我尝试在线阅读不同的内容以获取有关此问题的一些知识。
所以我厌倦了以下内容:
没有运气。
然后我尝试阅读此内容,发现此内容不适用于 Core 3.1。
我的问题是,究竟应该怎么做才能使 EditForm 像 UI 一样显示日期和数字,为什么 EditForm 会发生这种情况?
我知道default(bool?)回报null:
var msg = default(bool?) is null ?
"default(bool?) is NULL":
"default(bool?) is NOT NULL";
// msg == "default(bool?) is NULL"
Run Code Online (Sandbox Code Playgroud)
然而,当处理泛型类型参数时,它会导致令人困惑的结果。
考虑如下通用方法:
static TValue? GetProcItem<TValue>(IDictionary<string, JToken> cache, string key)
{
if(cache.TryGetValue(key, out var jtoken))
{
var value = jtoken.ToObject<TValue>();
return value;
}
// if the key does not exist
TValue? ret = default(TValue?);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码返回default(TValue?)不适用于bool指定的泛型类型:
var cache = new Dictionary<string, JToken>();
// the compiler infers that xBool is a …Run Code Online (Sandbox Code Playgroud) 诸如此类的语言C#允许我们编写如下代码:
// C#
Console.WriteLine($"{3 > 2.3}"); // compare an int with a double
Run Code Online (Sandbox Code Playgroud)
但是F#不允许我们这样做。例如,
// F#
printfn "%A" (3 > 4) // fine
printfn "%A" (3.0 > 4.0) // fine
printfn "%A" (3 > 4.0) // wrong
Run Code Online (Sandbox Code Playgroud)
是否有特别的原因,为什么一些二进制运营商F#一样(+),(>),(<) 只接受参数必须是同一类型?
我有一个在 VS 2019 中创建的带有 .net core 3.1 的 Wep Api 项目。为了测试,我将其部署到本地目录并从 doployed 文件夹启动 exe。请参阅使用 Visual Studio 将应用程序部署到本地文件夹。工作正常,但 api 始终监听端口 5000/50001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
Run Code Online (Sandbox Code Playgroud)
在哪里更改端口?项目的Properties/launchSettings.json中的设置似乎没有影响(仅用于从VS调试)?
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5101",
"sslPort": 5101
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"commandLineArgs": "Authority=http://localhost:5100",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Api": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}, …Run Code Online (Sandbox Code Playgroud) 我创建了默认的 blazor 服务器端应用程序。然后添加Microsoft.AspNetCore.SignalR.Client和ChatHub类。然后编辑startup.cs文件(添加services.AddSignalR()和endpoints.MapHub<ChatHub>("/chatHub"))和index.razor页面。然后通过IIS Express运行。没关系。
然后添加docker支持并运行Docker主机。它不工作。因为只有集线器连接 StartAsync 方法不起作用。如何运行它?帮我?非常感谢你们。
错误是:
处理请求时发生未处理的异常。SocketException:无法分配请求的地址System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,int端口,CancellationToken取消令牌)
HttpRequestException:无法分配请求的地址System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,int端口,CancellationToken取消令牌)
索引.剃刀代码:
@code {
private HubConnection _hubConnection;
protected override async Task OnInitializedAsync()
{
_hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/chatHub"))
.Build();
_hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
var encodedMsg = $"{user}: {message}";
StateHasChanged();
});
await _hubConnection.StartAsync(); // **DON'T WORK IN DOCKER HOST.**
}
}
Run Code Online (Sandbox Code Playgroud)
Docker 文件:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["BlazorApp1/BlazorApp1.csproj", "BlazorApp1/"] …Run Code Online (Sandbox Code Playgroud) asp.net-core ×8
blazor ×5
c# ×4
.net-core ×3
.net ×2
docker ×1
f# ×1
html ×1
localization ×1
mono ×1
signalr ×1
webassembly ×1