首先,我读了this question和this question 我想我还有另一个问题。因为一切都是一样的。
我有MVC5项目。我有2个区域。 首先,我的默认根类是这样的。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional});
}
第一个区域路线配置。
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"App_default",
"App/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
此时,没有问题。但我想这样:
此应用程序/控制器
public class AccountsController : Controller
{
// GET: App/Accounts
[Route("app/accounts/list/{Id}")]
public ActionResult List()
{
return View();
}
}
现在,我可以访问:
如何使用区域路由属性?我不能这样做?
答案 0 :(得分:1)
您需要在List ActionResult中接收一个整数作为参数,以便'localhost / app / accounts / list / 45646'URL能够正常工作。
new Tb();
答案 1 :(得分:0)
首先,您需要启用属性路由
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Enable attribute routing
routes.MapMvcAttributeRoutes();
//Area registration should be done after
//attribute routes to avoid route conflicts
AreaRegistration.RegisterAllAreas();
//convention-based routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional}
);
}
然后将适当的属性添加到控制器
[RouteArea("AreaName", AreaPrefix = "app/accounts")]
public class AccountsController : Controller {
[HttpGet]
[Route("list/{id:int}")] // Matches GET app/accounts/list/45646
public ActionResult List(int id) {
return View();
}
}