我正在尝试在 .net core 3 MVC 项目中使用 Blazor。我使用了一些教程来做到这一点,比如https://fizzylogic.nl/2019/08/18/integrating-blazor-in-an-existing-asp-net-core-application/和https://chrissinty.com /using-blazor-components-in-an-existing-mvc-application/。
工作原理:Blazor 组件的初始化工作正常。OnInitializedAsync() 函数被触发,组件渲染得很好。
这是来自 MVC 视图的调用:
@(await Html.RenderComponentAsync<MyProject.Components.Locations>(RenderMode.ServerPrerendered))
Run Code Online (Sandbox Code Playgroud)
什么不起作用:在 Blazor 组件中使用 @onclick。这是一些示例代码:
<span @onclick="AddLocation"></span>
@code {
private void AddLocation(){
}
}
Run Code Online (Sandbox Code Playgroud)
它可能不起作用的原因是我在连接初始化时出错。组件不是从基本路径渲染的,而是从特定视图的 url 渲染的,我认为这会影响连接。我在 Chrome 的控制台中收到这些错误:
主要的错误可能是'将'_blazor '标准化为' https://localhost:44375/Locations/_blazor ' '。从基本路径请求 _blazor 有效。有谁知道在这种情况下如何设置基本路径?
(是的,普通 Razor 页面环境中的 …
我正在为 Asp.net 核心 3.0 Blazor 服务器端应用程序构建示例登录 razor 组件。每当代码到达 SignInAsyc 方法时,它似乎只是挂起或锁定,因为代码停止进一步执行。我还尝试通过使用 PasswordSignInAsync 方法来切换逻辑,这给了我完全相同的结果。所有代码都将在该方法之前执行,但在执行该语句时会冻结。我在这里缺少什么?
Razor 组件页面:
<div class="text-center">
<Login FieldsetAttr="fieldsetAttr" UsernameAttr="usernameAttr" PasswordAttr="passwordInput"
ButtonAttr="buttonAttr" ButtonText="Sign In" InvalidAttr="invalidAttr" />
</div>
@code {
Dictionary<string, object> fieldsetAttr =
new Dictionary<string, object>()
{
{"class", "form-group" }
};
Dictionary<string, object> usernameAttr =
new Dictionary<string, object>()
{
{"class", "form-control" },
{"type", "text" },
{"placeholder", "Enter your user name here." }
};
Dictionary<string, object> passwordInput =
new Dictionary<string, object>()
{
{"class", "form-control" },
{"type", "password" }
};
Dictionary<string, object> buttonAttr …Run Code Online (Sandbox Code Playgroud) 我尝试实现像此示例这样的简单onclick事件:https : //blazorfiddle.com/s/counter,但是在我的解决方案中不起作用。该事件仅出于未知原因在网页运行时触发。
带有blazor组件的HTML页面显示得很好,但是当我单击按钮时,什么也没有发生。
我正在使用VS 2019 .Net Core 3.0。ASP.NET MVC项目
Counter.razor文件:
@using Microsoft.AspNetCore.Components
<p>Current count: @currentCount</p>
<button class="btn btn-primary" onclick="@IncrementCount();">Click me</button>
@code {
int currentCount = 0;
private async Task IncrementCount()
{
await Task.Run(() => currentCount++);
}
}
Run Code Online (Sandbox Code Playgroud)
Index.cshtml:
@using WebApplication2.Views.Components
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
@(await Html.RenderComponentAsync<Counter>(RenderMode.Server, new { }))
Run Code Online (Sandbox Code Playgroud)
startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddHttpClient();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
} …Run Code Online (Sandbox Code Playgroud)