【发布时间】:2021-10-28 21:52:03
【问题描述】:
我正在将旧版应用程序移植到 ASP.NET Core,并尝试保持 URL 一致。理想情况下,我需要在 URL 中包含“文件扩展名”,例如/Home/Index.ext 应该路由到对HomeController 的Index 操作。
我从 Visual Studio 中的标准 Web 应用程序模板开始,并尝试修改默认路由:
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}.ext/{id?}");
对/Home/Index.ext 的请求然后给出错误:
An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:
WebApplication1.Controllers.HomeController.Index (WebApplication1)
WebApplication1.Controllers.HomeController.Privacy (WebApplication1)
WebApplication1.Controllers.HomeController.Error (WebApplication1)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
我不明白为什么它没有从该 URL 中获取操作名称来选择正确的操作。有什么方法可以更改此路由模式以使其正确匹配?
【问题讨论】: