【问题标题】:Routing Issue, Index Action Must Be Explicitly Called?路由问题,必须显式调用索引操作?
【发布时间】:2013-09-25 03:49:24
【问题描述】:

我在 MVC 项目中使用 LowerCasedDashedRoutes 包并写了这一行...

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.Add(new LowercaseDashedRoute("{action}",
    new RouteValueDictionary(
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
    new DashedRouteHandler()
  )
);

当我想从 URL 中删除 Home 控制器名称并简单地在该控制器中调用我想要的操作时,这非常有用。但是,当我想使用 http://www.mydomain.com/codes 之类的 URL 去另一个控制器说 Codes 时,我想要 Index 的默认操作strong>代码 控制器显示。相反,除非我输入 http://www.mydomain.com/codes/index,否则我会得到 404,我需要写什么才能让它工作?

【问题讨论】:

  • 为此您需要定义一条新路线
  • 听起来不错,你能给我一些这条新路线的代码吗?
  • 你能帮忙写代码吗?

标签: .net asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

添加类似这条路线的东西

routes.MapRoute(
  "Codes",
  "codes/{id}",
  new { Controller = "codes", action = "Index", id= UrlParameter.Optional },
  new[] { "YourProjectName.Controller" } // namespace of the code controller
);

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多