.NET 6部署Azure App Service遇HTTP 500错误:路由优先级重复
问题解决:Azure部署后.NET 6应用HTTP 500错误,重启恢复
问题背景
已将AngularJS应用迁移至.NET 6,功能运行正常,但每次通过Azure CI/CD Pipeline部署到Azure App Service时,都会出现HTTP 500错误,重启App Service后即可恢复正常。
异常详情
2022-07-26 18:43:18.397 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1 GET http://YOUR_WEBSITE_NAME.azurewebsites.net/ - - 2022-07-26 18:43:18.413 +00:00 [Error] Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request. System.InvalidOperationException: Using ExpandEndpoint requires that the replaced endpoint have a unique priority. The following endpoints were found with the same priority: /Index /Index /Index at Microsoft.AspNetCore.Routing.Matching.CandidateSet.ValidateUniqueScore(Int32 index) at Microsoft.AspNetCore.Routing.Matching.CandidateSet.ExpandEndpoint(Int32 index, IReadOnlyList`1 endpoints, IComparer`1 comparer) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DynamicPageEndpointMatcherPolicy.ApplyAsync(HttpContext httpContext, CandidateSet candidates) at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
当前Startup.cs配置
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddMvc() .AddRazorPagesOptions(options => { // Match all routes to the index so we can handle routing client-side. options.Conventions.AddPageRoute("/index", "{*url}"); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapFallbackToPage("/Index"); }); } }
问题原因
路由配置重复导致端点优先级冲突:
services.AddRazorPages()自动注册了/Index页面的默认路由options.Conventions.AddPageRoute("/index", "{*url}")给/Index页面额外添加了通配符路由endpoints.MapFallbackToPage("/Index")又添加了一条fallback路由指向/Index
这三个配置生成了多个优先级相同的/Index端点,部署后路由匹配器无法区分,抛出异常;重启App Service时路由系统重新初始化,临时规避了冲突,但并未解决根本问题。
解决方案
简化路由配置,保留一种方式让所有请求指向/Index页面,避免重复注册:
方案一:移除冗余Mvc配置,保留Razor Pages和Fallback路由
修改ConfigureServices方法,去掉多余的AddMvc()配置(AddRazorPages()已包含必要的Mvc基础功能):
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); }
Configure方法保留原有的MapRazorPages()和MapFallbackToPage("/Index")即可,fallback路由会将所有未匹配的请求导向Index页面,满足AngularJS客户端路由需求。
方案二:仅使用通配符路由,去掉Fallback配置
如果需要保留通配符路由,调整配置如下:
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(options => { options.Conventions.AddPageRoute("/Index", "{*url}"); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // 其他中间件配置不变 app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); // 移除 endpoints.MapFallbackToPage("/Index"); }); }
通配符路由会让所有请求直接匹配到Index页面,无需额外fallback配置。
内容的提问来源于stack exchange,提问作者Ravi Mittal
相关产品推荐
相关产品推荐

