MVC 路由误区
我正在开发 ASP.NET MVC 应用程序。由于某种原因,每次我认为我了解路由时,就会弹出一些我不理解的内容。目前,我有两条路线我似乎无法弄清楚。我的目录结构如下所示
- Views
- Internal
- Profile
- Index.cshtml
- Input
- Page1.cshtml
在我的 global.asax.cs 文件中,我添加了以下映射:
routes.MapRoute(
"UserProfileInfo",
"{controller}/profile",
new { controller = "Internal", action = "UserProfileInfo" }
);
routes.MapRoute(
"Page1",
"{controller}/input/page1",
new { controller = "Internal", action = "Page1" }
);
在 MyController 中,我有以下内容:
public ActionResult UserProfileInfo()
{
return View("~/Views/internal/profile/Index.cshtml");
}
public ActionResult Page1()
{
return View("~/Views/internal/input/Page1.cshtml");
}
我想将我的操作存储在单个控制器中。我以为我已经正确设置了一切。但我仍然收到 404 错误。我做错了什么?
I am working on an ASP.NET MVC application. For some reason, everytime I think I understand routing, something pops up that I don't understand. Currently, I have two routes that I can't seem to figure out. My directory structure looks like the following
- Views
- Internal
- Profile
- Index.cshtml
- Input
- Page1.cshtml
In my global.asax.cs file, I have added the following mappings:
routes.MapRoute(
"UserProfileInfo",
"{controller}/profile",
new { controller = "Internal", action = "UserProfileInfo" }
);
routes.MapRoute(
"Page1",
"{controller}/input/page1",
new { controller = "Internal", action = "Page1" }
);
In MyController, I have the following:
public ActionResult UserProfileInfo()
{
return View("~/Views/internal/profile/Index.cshtml");
}
public ActionResult Page1()
{
return View("~/Views/internal/input/Page1.cshtml");
}
I want to store my actions in a single controller. I thought I had everything setup properly. But I continue to get a 404. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用 MapRoute 时从控制器名称中删除“Controller”后缀,以创建到名为
InternalController
的类的映射。当寻找匹配的实现时,控制器后缀由框架附加。例如:Remove the "Controller" suffix from the controller name in your calls to MapRoute to create a mapping to a class called
InternalController
. The Controller suffix is appended by the framework when looking for a matching implementation. e.g.: