将 blazor 组件包含到 MVC 视图中(.NET Core 3 Preview #5)

Tob*_*obi 2 blazor

我正在尝试将 blazor 组件包含到我的 MVC 应用程序中,但端点路由的某些内容似乎不太好。

我有一个 razor 页面(在 pages/example.cshtml 中)和一个带有视图(在 Views/Home/Index.cshtml 中)的默认控制器(在 Controllers/Home 中)。

开...

  • [本地] -> 索引视图,Blazor 有效
  • [local]/Home -> 索引视图,Blazor 工作
  • [local]/Example -> 示例页面,Blazor 有效
  • [local]/Home/ -> 索引视图,Blazor 不起作用
  • [local]/Home/Index -> 索引视图,Blazor 不起作用

脚本调试器说: HTTP404:未找到 - 服务器未找到与请求的 URI(统一资源标识符)匹配的任何内容。(XHR)POST - https://localhost:44342/Home/_blazor/negotiate

我在启动文件中尝试了不同的东西,但无论我尝试过什么,我都无法让它工作。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();
        //services.AddControllersWithViews(o => o.EnableEndpointRouting = false); -> does not change anything
        services.AddMvc(o => o.EnableEndpointRouting = false);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseStaticFiles();

        app.UseMvcWithDefaultRoute();
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapBlazorHub();
            //endpoints.MapControllers();  -> does not change anything
            //endpoints.MapDefaultControllerRoute(); -> does not change anything
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

hel*_*rve 5

GitHub 上的问题已由其中一位开发人员解决:https : //github.com/aspnet/AspNetCore/issues/13594#issuecomment-527821142

这是设计使然。您需要在 html 页面<base href="~/" />head标签内使用设置文档的基本路径。或者,您可以手动启动 blazor,如

    <script src="_framework/blazor.server.js" autostart="false"></script>
    <script>
        Blazor.start({
            configureSignalR: function (builder) {
                builder.useUrl(@("/"))
            }
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

尽管这第二种解决方案可能会阻止链接正常工作。