我刚开始在我的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
< - no'index'子路由部分。
可以使用AttributeRouting完成吗?
我知道如何使用默认的ASP.NET MVC3结构等来做这件事。我想要做的是通过AttributeRouting来解决这个问题。
答案 0 :(得分: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();
}
}
一点解释:
希望这会有所帮助。如果我对您尝试做的事情的初步假设不正确,请发表评论。