0

I have 2 controllers:

public class HomeController : Controller
    {
        public ActionResult Index(string id) //id is category slug
        {
           if(string.IsNullOrEmpty(id))
               id = "MainCategory";
           var model = getCategoryPageModel(id);
           return View(model);
        }
    }
public class PostController : Controller
    {
        public ActionResult Index(string id) //id is post slug
        {
          var model = getPostModel(id);
          return View(model);
        }
    }

And this is my route config:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //angular route for admin section
            routes.MapRoute(
               name: "AngularCatchAllRoute",
               url: "Admin/{*any}",
               defaults: new { controller = "Admin", action = "Index"}
               );

            //route which includes language
            routes.MapRoute(
                name: "DefaultLocalized",
                url: "{lang}/{controller}/{action}/{id}",
                constraints: new { lang = @"(\w{2})|(\w{2}-\w{2})" },   // en or en-US
                defaults: new { controller = "Home", action = "Index", id = "" }
            );

            //do I need this one??
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

I want to have two types of custom routes:

www.mysite.xyz/categoryName - calls Home/Index/id with default language ('sr') www.mysite.xyz/en/categoryName - calls Home/Index/id with language 'en'

www.mysite.xyz/Post/postID - calls Post/Index/id with default language('sr') www.mysite.xyz/en/Post/postID - calls Post/Index/id with language 'en'

My 'DefaultLocalized' route already works fine with default and custom language route part, but my url has to contain all route parts: controller/action/id. I just want to simplyfy urls to be more readable to users.

Actually I made it work for post with 'lang' to be mandatory: www.mysite.xyz/sr/Post/postID - but I want 'sr' to be default like in 'DefaultLocalized' route, there I don't have to set lang to be 'sr'...

I already have tried some answers from other similar questions but I did not make it work.

vpetrovic
  • 527
  • 4
  • 22
  • Possible duplicate of [ASP.NET MVC 5 culture in route and url](http://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url) – NightOwl888 Aug 11 '16 at 08:55

1 Answers1

0

You can use attribute routing as below:

[Route("~/{cate}")]
[Route("{lang}/{cate}")]
public IActionResult Index(string lang, string cate)
{
   return View();
}

That work for me with urls:

http://[host]/Mobile

http://[host]/en/Mobile

Hope this help!

Ha Hoang
  • 1,644
  • 12
  • 14
  • I tried to avoid using 'lang' as action parameter, I folowed this post for multi-language site: http://www.ryadel.com/en/setup-a-multi-language-website-using-asp-net-mvc/ – vpetrovic Aug 11 '16 at 07:23
  • I'm not sure but that can be useful for you at here: http://afana.me/post/aspnet-mvc-internationalization.aspx – Ha Hoang Aug 11 '16 at 07:41