将blazor组件包括到MVC视图中(.NET Core 3预览#5)

时间:2019-05-10 12:04:53

标签: blazor

我正在尝试在我的MVC应用程序中包含一个blazor组件,但是使用端点路由的某些东西似乎并不可行。

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

正在打开...

  • [本地]->索引视图,Blazor工作正常
  • [local] / Home->索引视图,Blazor可以使用
  • [本地] /示例->示例页面,Blazor工作正常
  • [local] / Home /->索引视图,Blazor无法正常工作
  • [本地] /首页/索引->索引视图,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
        });
    }
}

1 个答案:

答案 0 :(得分:1)

其中一个开发人员已解决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>

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

相关问题