Identity Razor 页面中的 Blazor 组件

4

我有一个服务器端的 Blazor 应用程序,按照 Visual Studio 模板建议设置了身份验证。然后,我添加了标识脚手架项。现在,我尝试将 razor 组件添加到登录屏幕上,以便可以在同一设计中保留所有内容。

在页面本身上,我添加了以下内容:

<script src="~/_framework/blazor.server.js"></script>
<Login>
    @(await Html.RenderComponentAsync<ApplySupportTool.Blazor.Pages.Shared.Login>(RenderMode.Server))
</Login>

现在由于身份页面在其自己的进程中运行(基于ScaffoldingReadme),这是在IdentityHostingStartup.cs中设置的,我尝试在那里添加Blazor设置:

        builder.ConfigureServices((context, services) => { 
        });

        builder.UseStartup<IdentityStartup>();

public class IdentityStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddServerSideBlazor();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapBlazorHub();
        });
    }
}

但是,一旦我这样做,当我尝试在programm.cs中解析我的DB上下文时,就会出现空指针异常。有趣的是,即使在IdentityStartup中只有空方法体时,也会发生这种情况。另外,我试图将所有的DB初始化代码移动到启动文件中以避免空指针异常,但是网站就不能启动了。 我需要在不同的地方或者以不同的方式设置吗?


你找到解决方案了吗? - Nicolas
很遗憾,不行。 - NPadrutt
很遗憾,我也有类似的问题。由于它还有其他错误/问题(https://stackoverflow.com/questions/62203126/using-blazor-components-from-a-shared-library-in-an-asp-net-core-web-application?noredirect=1#comment110012450_62203126),所以我为此创建了自己的帖子。最终,你是使用普通的CSS / cshtml标记而不是组件吗? - Nicolas
是的,最终我生成了Identity Razor页面并相应地进行了样式设计。这有点不幸,因为我必须使用另一个框架(因为Blazor显然无法工作)。所以它看起来略有不同。 - NPadrutt
1个回答

0

你缺少一些配置DBContext的代码。

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<BloggingContext>(options => options.UseSqlite("Data Source=blog.db"));
}

请参阅配置 DbContext 的更多细节


请注意,此时我不进行任何数据库访问。Blazor组件只是显示文本。 - NPadrutt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接