Reputation: 3930
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 }
);
Upvotes: 1
Views: 3302
Reputation: 23
/* 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 });
Upvotes: 0
Reputation: 3930
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?)
Upvotes: 2