I'm working with multitenant .net core web application which consists of WebApi and MVC routing. With normal behavior application should go to fallback controller, where using external help will be decided on which of master/tenant controller will be performed redirect.But sometimes application refuses to find proper fallback to action. After some investigation, I've found that at these moments, not all endpoints are being registered. After two days of search, I figured out that most of these problems affect Razor, but none of them are applicable to MVC/WebApi. I start to think that this is caused by having second branch with .MapWhen() but its not confirmed.
So I'd appreciate any help on solving this problem. Also attaching my current configuration of routing down below:
ConfigureServices method:
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));
});
Configure method:
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");
});
});
Example of exception:
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)
endpoints.MapFallbackToController("Index", "Fallback");so it will scan all existing endpoints, and if none fits, it should go to Fallback controller's Index action. but this actions is not being registered as endpoint somehow - Hephaestus901