我正在尝试将 blazor 组件包含到我的 MVC 应用程序中,但端点路由的某些内容似乎不太好。
我有一个 Razor 页面(在pages/example.cshtml中)和一个带有 View (在Views/Home/Index.cshtml中)的默认 Controller (在Controllers/Home中)。
正在打开...
- [本地] -> 索引 View ,Blazor 有效
- [本地]/Home -> 索引 View ,Blazor 有效
- [本地]/示例 -> 示例页面,Blazor 有效
- [local]/Home/-> 索引 View ,Blazor 不起作用
- [local]/Home/Index -> 索引 View ,Blazor 不起作用
脚本调试器说: HTTP404: NOT FOUND - 服务器未找到与请求的 URI(统一资源标识符)匹配的任何内容。 (XHR)发布-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
});
}
}
最佳答案
GitHub 上的问题已由一位开发人员解决:https://github.com/aspnet/AspNetCore/issues/13594#issuecomment-527821142
这是设计使然。您需要使用 <base href="~/" />
设置文档的基本路径里面head
你的 html 页面的标签。或者,您可以手动启动 blazor,如下所示
<script src="_framework/blazor.server.js" autostart="false"></script>
<script>
Blazor.start({
configureSignalR: function (builder) {
builder.useUrl(@("/"))
}
});
</script>
尽管第二种解决方案可能会阻止链接正常工作。
关于blazor - 将 blazor 组件包含到 MVC View 中(.NET Core 3 Preview #5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077391/