为什么路由在应该映射到不同的控制器时调用 HomeController Index?

Why is routing calling HomeController Index when it should be mapped to different controller?

我想要 url https://localhost:44354/Bills to call the Index action in the BillsController.cs. Instead, it is calling the Index action from HomeController.cs. If I use https://localhost:44354/Bills/Bills 它调用 BillsController 的索引操作。

按照我的理解,Bills 应该映射到控制器,并且它应该默认为 Index,URL 中没有第二个参数。为了测试,我尝试了 https://localhost:44354/Bills/Index 给出了 404。有人可以解释这种行为吗?它的行为似乎不正确。

我的 Startup.cs 文件中的路由:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

账单控制器:

public class BillsController : Controller
    {    
        public IActionResult Index()
        {
            return View();
        }
    }

我的文件夹结构:

@NickOban,我尝试创建一个新的 MCV 项目并添加了一个账单控制器。然后我添加了 Bills 存储库并添加了一个 index.cshtml

路径对我有用。但是,您似乎在某个时候实施了名称为 "Bills" 的前缀,这会导致您遇到的现象。

确保您的代码中没有任何前缀。 MVC 应用程序应自动修复任何前缀。另外尝试 https://localhost:44354/Bills/Index.cshtml 而不是简单地使用 Index ~ 因为 MVC 会认为 Index 是一个文件夹。