【发布时间】:2020-02-23 10:50:22
【问题描述】:
最近遇到了一些意想不到的问题
我使用的是 ASP.NET Core 3.0,我在 StartUp.cs 中定义了两条路由
StartUp.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "file",
pattern: "{controller=File}/folder/{*path}",
new { Action = "Folder" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=File}/{action=Index}/{filename}");
});
文件控制器.cs
public class FileController : Controller
{
public IActionResult Folder(string path)
{
return Ok(path);
}
public IActionResult Index(string filename)
{
return Ok(filename);
}
}
请求 file/folder/abc/abc 我希望匹配第一条路线 但结果是 404 not found
但是如果我改变了路线的顺序
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=File}/{action=Index}/{filename}");
endpoints.MapControllerRoute(
name: "file",
pattern: "{controller=File}/folder/{*path}",
new { Action = "Folder" });
});
成功了!
如果我在顶部定义 {controller=File}/folder/{*path},我的问题是为什么第一个版本不起作用
我以为它会顺序检查路由表
【问题讨论】:
-
如果您使用第一个
UseEndpoints方法,然后请求/File/folder/abc/abc而不是/file/folder/abc/abc,会发生什么情况?尽管表面上不区分大小写,但路由可能表现出区分大小写的行为。 -
与第一个版本相同的结果
标签: asp.net-core