我正在使用由WebApi和MVC路由组成的多租户.net核心Web应用程序。正常情况下,应用程序应转到故障预置控制器,在该控制器上使用外部帮助来决定将执行哪个主/租户控制器重定向,但有时应用程序拒绝找到适当的故障预置。经过一番调查,我发现此时此刻,并非所有端点都已注册。经过两天的搜索,我发现这些问题大多数会影响Razor,但它们都不适用于MVC / WebApi。我开始认为这是由.MapWhen()的第二个分支引起的,但尚未得到确认。
因此,感谢您对解决此问题的任何帮助。还要在下面附加我当前的向下路由配置:
ConfigureServices方法:
services.AddControllersWithViews(options =>
{
options.Filters.Add(typeof(ReverseProxyFilter));
options.Conventions.Add(new ApiExplorerGroupPerVersionConvention());
})
.AddRazorRuntimeCompilation()
.AddApplicationPart(typeof(EmailNamespace).Assembly)
.AddViewLocalization()
.SetCompatibilityVersion(CompatibilityVersion.Latest)
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
})
.AddControllersAsServices();
services.AddRazorPages();
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
});
services.Configure<RouteOptions>(routeOptions =>
{
routeOptions.ConstraintMap.Add("master", typeof(MasterRouteConstraint));
routeOptions.ConstraintMap.Add("tenant", typeof(TenantRouteConstraint));
});
配置方法:
app.UseMiddleware<TenantFilterMiddleware>();
app.UseHttpStatusCodeExceptionMiddleware();
app.UseResponseCaching();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<WebsocketsHub>("/hubs");
endpoints.MapControllers();
endpoints.MapRazorPages();
});
app.MapWhen(
context => !context.Request.Path.Value.StartsWith("/api"),
builder =>
{
builder.UseRouting();
builder.UseEndpoints(endpoints =>
{
endpoints.MapFallbackToController("Index", "Fallback");
});
});
例外示例:
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot find the fallback endpoint specified by route values:
{
action: Index,
controller: Fallback,
area:
}.
Microsoft.AspNetCore.Mvc.Routing.DynamicControllerEndpointMatcherPolicy.ApplyAsync(HttpContext HttpContext, CandidateSet candidates)
答案 0 :(得分:1)
经过两天的尝试和研究,解决方案仍然像往常一样简单。 解决此问题的方法是稍微重写路由管道。据我所知,Microsoft路由系统非常希望将分支放置在默认分支之前,而不是在默认分支之后,在这种情况下,所有系统行为在100%的情况下都按预期开始工作。
所以不要这样:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<WebsocketsHub>("/hubs");
endpoints.MapControllers();
endpoints.MapRazorPages();
});
app.MapWhen(
context => !context.Request.Path.Value.StartsWith("/api"),
builder =>
{
builder.UseRouting();
builder.UseEndpoints(endpoints =>
{
endpoints.MapFallbackToController("Index", "Fallback");
});
});
现在我有了这个
app.MapWhen(context => !context.Request.Path.Value.StartsWith("/api"), ConfigureApiPipeline);
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(MapBasicEndpoints);
ConfigureApiPipeline()
函数如下所示:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
MapBasicEndpoints(endpoints);
endpoints.MapHealthChecks(SystemLivenessCheck.Path, new HealthCheckOptions()
{
AllowCachingResponses = false,
ResultStatusCodes = new Dictionary<HealthStatus, int>
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status503ServiceUnavailable,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
},
});
endpoints.MapFallbackToController("Index", "Fallback");
});
MapBasicEndpoints
是:
private static void MapBasicEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapHub<WebsocketsHub>("/hubs");
endpoints.MapControllers();
}