區域中的屬性路由

要在區域中使用屬性路由,需要註冊區域和 [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" })