gpt4 book ai didi

c# - 更改 Blazor 文件夹给我 "Cannot find the fallback endpoint"

转载 作者:行者123 更新时间:2023-12-04 01:38:44 27 4
gpt4 key购买 nike

我创建了一个默认的 Blazor 项目,并希望在同一个项目中有一个 Web API 部分用于原型(prototype)设计。

所以我将所有 Controller 和相关内容放在 Api 子文件夹中,将 Blazor 内容放在 Blazor 子文件夹中。

项目结构:

- Api
- Controllers
- AController.cs
- Blazor
- Pages
- _Host.cshtml
- Counter.razor
- Error.razor
- FetchData.razor
- Index.razor
- Data
- WeatherForecast.cs
- WeatherForecastService.cs
- Shared
- MainLayout.razor
- NavMenu.razor
- _Imports.razor
- App.razor
- appsettings.json
- appsettings.Development.json
- Program.cs
- Startup.cs

Startup.cs

public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "An API",
Version = "Version Yoohoo"
});

var xmlFileName = Assembly.GetExecutingAssembly().GetName().Name + ".xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFileName);

options.IncludeXmlComments(xmlPath);
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});

app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"));
}
}

但是现在启动应用程序时,出现以下异常:

An unhandled exception occurred while processing the request. InvalidOperationException: Cannot find the fallback endpoint specified by route values: { page: /_Host, area: }. Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DynamicPageEndpointMatcherPolicy.ApplyAsync(HttpContext httpContext, CandidateSet candidates)

我该如何解决这个问题?

最佳答案

您还需要更改 Razor Pages 的默认根目录,尝试在启动时添加以下代码 ConfigureServices

services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/Blazor/Pages");

关于c# - 更改 Blazor 文件夹给我 "Cannot find the fallback endpoint",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com