0

My url is as follow: www.example/videos/category1,category2,category3/sex/hospital/doctor But in my url category1 and category2 and category3(may be 1 or too many) can be null/empty. Also sex or hospital or doctor also can be null. How can i detect which one is null. If categories is null. Then mvc sense sex as categories and hospital as sex. What kind of approach i can use?

tereško
  • 58,060
  • 25
  • 98
  • 150
ftdeveloper
  • 1,053
  • 3
  • 26
  • 50

1 Answers1

1

MVC cannot tell if an earlier URL part is null over it being a different route.

If your route is:

videos/{categories}/{subcategory1}/{subcategory2}/{subcategory3}/

All are optional. But if categories are different than the other subcategories, you'll need to have a placeholder for when it is null. Basically you'll need another route for when categories are null.

context.MapRoute(
    "video_nocategories",
    "videos/all/{subcategory1}/{subcategory2}/{subcategory3}/",
    new
    {
        action = "Index",
        controller = "Video",
        categories = "",
        subcategory1 = UrlParameter.Optional,
        subcategory2 = UrlParameter.Optional,
        subcategory3 = UrlParameter.Optional,
    }
);

context.MapRoute(
    "video_categories",
    "videos/{categories}/{subcategory1}/{subcategory2}/{subcategory3}/",
    new { 
        action = "Index", 
        controller = "Video",
        subcategory1 = UrlParameter.Optional,
        subcategory2 = UrlParameter.Optional,
        subcategory3 = UrlParameter.Optional,
    }
);
smdrager
  • 7,327
  • 6
  • 39
  • 49