我需要将cultureRoute Route链接到用[Route(“{urlSlug}”)]属性修饰的控制器中的各种方法。
这是我的RouteConfig文件
using System.Web.Mvc;
using System.Web.Mvc.Routing.Constraints;
using System.Web.Routing;
namespace Site.Web
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "cultureRoute",
url: "{culture}/{controller}/{urlSlug}",
defaults: new { controller = "Home", urlSlug = UrlParameter.Optional },
constraints: new
{
culture = new RegexRouteConstraint("^[a-z]{2}(?:-[A-Z]{2})?$")
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{urlSlug}",
defaults: new { culture = "en-GB", controller = "Home", action = "Index", urlSlug = UrlParameter.Optional }
);
}
}
}
我的大多数控制器都有这种结构
[RoutePrefix("Branded")]
public class BrandedController : ControllerBase
{
[KenticoCacheFilter]
public async Task<ActionResult> Index()
{
return View("../../Views/Project/Index", viewModel);
}
[Route("{urlSlug}")]
public async Task<ActionResult> Project(string urlSlug)
{
return View("../../Views/Project/Individual", viewModel);
}
// Or
[Route("{urlSlug}")]
public async Task<ActionResult> Article(string urlSlug)
{
return View("../../Views/Project/Individual", viewModel);
}
}
那么,如何将Project()或Article()方法链接到CultureRoute,因为它需要在RouteConfig中显式设置方法?