1

I need to link cultureRoute Route to various methods in the Controllers decorated with [Route("{urlSlug}")] attribute.

Here is my RouteConfig file

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 }
        );
    }
}

}

And most of my Controllers have this structure

    [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);
        }

    }

So, how do I link the Project() or Article() methods to the CultureRoute as it requires to set the method explicitly in the RouteConfig ?

Alex
  • 431
  • 1
  • 6
  • 14
  • 1
    See [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/a/32839796). You can reconfigure attribute routing to add an additional route with culture for each route you declare. However, you should also read [Why map special routes first before common routes in asp.net mvc?](https://stackoverflow.com/a/35674633) because having two routes that overlap the same range of URLs is not possible without constraining them in some way. If you don't want to put the primary key in the URL, consider 2 [custom CMS routes](https://stackoverflow.com/a/31958586). – NightOwl888 Mar 20 '18 at 15:28

0 Answers0