【问题标题】:MVC Routing MisunderstandingMVC 路由误区
【发布时间】:2012-03-10 19:43:39
【问题描述】:

我正在开发一个 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。我做错了什么?

【问题讨论】:

标签: c# asp.net-mvc-3 routing controller


【解决方案1】:

在调用 MapRoute 时从控制器名称中删除“Controller”后缀,以创建到名为 InternalController 的类的映射。在寻找匹配的实现时,框架会附加 Controller 后缀。例如:

    routes.MapRoute( 
    "UserProfileInfo", 
    "{controller}/profile", 
    new { controller = "Internal", action = "UserProfileInfo" } 
); 

【讨论】:

  • 好吧,它们不在我的实际代码中。就在这篇文章中。我已经更新了帖子。感谢您指出这一点。
猜你喜欢
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2010-12-09
相关资源
最近更新 更多