区域中的属性路由

要在区域中使用属性路由,需要注册区域和 [RouteArea(…)] 定义。

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        AreaRegistration.RegisterAllAreas();
    }
}

在示例区域控制器属性路由定义中:

[RouteArea("AreaName", AreaPrefix = "AreaName")]
[RoutePrefix("SampleAreaController")]
public class SampleAreaController : Controller
{
    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

在区域中使用 Url.Action 链接:

@Url.Action("Index", "SampleAreaController", new { area = "AreaName" })