gpt4 book ai didi

model-view-controller - MVC Controller 索引有时是 404?

转载 作者:行者123 更新时间:2023-12-04 08:37:56 25 4
gpt4 key购买 nike

我添加了 3 个 Controller ,对于其中一个,默认情况下索引 View 不起作用。

Works(shows index):
http://localhost:1767/Employees/
http://localhost:1767/Employees/Index
http://localhost:1767/Home/
http://localhost:1767/Home/Index
http://localhost:1767/
http://localhost:1767/Companies/Index

Doesn't work(gives 404 error):
http://localhost:1767/Companies/

1) 我创建了一个 Entity Framework 类库,它是从包含员工和公司表的数据库生成的。

2)创建了一个MVC 3空项目。

3)增加了从MVC项目到EF类库项目的项目引用。

4)添加了一个 Controller ,在添加 Controller 对话框中,我选择了 EF 模型和其中一个表,并将 Controller 命名为 CompaniesController

5)然后我想起我需要一个 HomeController,所以我添加了另一个 Controller ,命名为 HomeController,并再次选择 Companies 实体。

6)最后我添加了一个EmployeesController 选择Employees 实体。

这是我的 Global.asax.cs 中唯一的路径:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

为什么不 http://localhost:1767/Companies/ URL 默认为索引操作,就像所有其他 Controller 一样?我做过一些实践MVC项目,似乎总是有这个问题。员工 Controller 默认为 Index 工作正常,即使没有路由告诉它这样做?

编辑:这是我收到的错误:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Companies/

编辑:所以我通常不会在在线发布示例时透露我的实体名称或域详细信息,但在这种情况下,公司实际上被称为属性,并将 View 文件夹和 Controller 的名称更改为 Properties2 解决了该问题。所以看起来 Properties 是一个特殊的关键字,如果它出现在 URL 中,它会打破默认的 Index

最佳答案

将 View 文件夹和 Controller 从 Properties(Controller) 更改为 Properties2(Controller),现在工作正常。显然,“属性”在 URL 中有一些特殊处理,并打破了对索引的默认设置

关于model-view-controller - MVC Controller 索引有时是 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6671974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com