1

I have a site structured like this -

  • Home page - www.mysite.com.

  • Categories page - www.mysite.com/Categories. (linked from Home page)

On the Categories page, I have a list of Category links.

If someone selects a Category, I want to display the URL like this -

  • (after selecting CategoryA) - www.mysite.com/Categories/CategoryA

  • (after selecting CategoryB) - www.mysite.com/Categories/CategoryB

Currently, my URLs display like this - www.mysite.com/Categories/Index/CategoryA

I have a HomeController and a CategoriesController. The CategoriesController has an Action called Index, which excepts a Category, which is where the word Index is coming from.

How can I get rid of the word Index?

I tried mapping a Route, like the following, but it did not work -

        routes.MapRoute(
            name: "CategoriesPage",
            url: "Categories/{id}",
            defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional }
        );
A Bogus
  • 3,852
  • 11
  • 39
  • 58
  • See here http://stackoverflow.com/a/3628138/426422 and here http://stackoverflow.com/questions/1713379/asp-net-mvc-routing-with-default-controller – Mike Cheel Jul 26 '13 at 18:30
  • Thanks Mike! I ended up finding the answer else where. (see my answer below) – A Bogus Jul 26 '13 at 19:09

2 Answers2

2

My solution was the following -

I mapped a Route like this -

        routes.MapRoute(
            "CategoryPage",
            "Categories/{id}",
            new { controller = "Categories", action = "Index" }
        );

And in my View, my ActionLink looked like this -

@Html.ActionLink("CategoryDescription", "Index", new { controller = "Categories", id = c.Id })

(this answer lead me to this solution - Routing: How to hide action name in url?)

Community
  • 1
  • 1
A Bogus
  • 3,852
  • 11
  • 39
  • 58
0

/* This is For Action Name Only /
routes.MapRoute("custom_name1", "sitemap", new { controller = "home", action = "sitemap", id = UrlParameter.Optional }); /
This is For Controller Name Only */
routes.MapRoute("custom_name2", "home", new { controller = "home", action = "sitemap", id = UrlParameter.Optional });

Vijay S
  • 23
  • 3