Is there a way to have a RoutePrefix that starts with an optional parameter?

前端 未结 4 1948
旧巷少年郎
旧巷少年郎 2020-12-20 16:26

I want to reach the Bikes controller with these URL\'s:

/bikes     // (default path for US)
/ca/bikes  // (path for Canada)

One way of achi

4条回答
  •  醉酒成梦
    2020-12-20 17:13

    You could use attribute routes with two ordered options.

    public partial class GlossaryController : Controller {
    
        [Route("~/glossary", Order = 2)]
        [Route("~/{countryCode}/glossary", Order = 1)]
        public virtual ActionResult Index()
        {
          return View();
        }
    }
    

    If you're planning to have region specific routes for all your pages you could add a route to the route config above the default. This will work only for views/controllers without attribute routes.

      routes.MapRoute(
         name: "Region",
         url: "{countryCode}/{controller}/{action}/{id}",
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
         constraints: new { countryCode = @"\w{2}" }
      );
    

提交回复
热议问题