5

我刚刚开始在我的 ASP.NET MVC3 应用程序中使用AttributeRouting 。我一开始就使用 -no- 控制器。(新的空 MVC3 应用程序)

然后我做了一个区域。(称为Documentation:)

然后我添加了一个控制器(称为DocumentationController:)

然后我做了这个。。

[RouteArea("Documentation")]
public class DocumentationController : Controller
{
    [GET("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

以下路线有效:/documentation/index

但是我怎样才能使这两条路线起作用?

1 - /<-- (默认路由/未指定特定路由) 2 - /documentation<-- 未添加“索引”子路由部分。

这可以用AttributeRouting完成吗?

更新:

我知道如何使用默认的 ASP.NET MVC3 结构等来做到这一点。我想做的是通过 AttributeRouting 来解决这个问题。

4

1 回答 1

9

我假设您希望“/”和“/documentation”映射到 DocumentationController.Index,是吗?如果是这样,请执行以下操作:

[RouteArea("Documentation")]
public class DocumentationController : Controller
{
    [GET("Index", Order = 1)] // will handle "/documentation/index"
    [GET("")] // will handle "/documentation"
    [GET("", IsAbsoluteUrl = true)] // will handle "/"
    public ActionResult Index()
    {
        return View();
    }
}

一点解释:

  • GET("Index") 的 Order = 1 将其标记为操作的主要路线。由于反射的工作原理,如果不使用 Order 属性,就无法确定动作上的属性顺序。看这里
  • 您可以将多个 get 路由映射到单个操作。看这里
  • IsAbsoluteUrl 属性允许您覆盖由 RouteArea 和 RoutePrefix 属性添加的 URL 前缀。这样最终路由将匹配根请求。看这里

希望这可以帮助。如果我最初对您尝试做的假设不正确,请发表评论。

于 2012-02-19T20:09:52.147 回答