A Bogus
A Bogus

Reputation: 3930

Removing Action name from URL

I have a site structured like this -

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

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

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

Answers (2)

Vijay S
Vijay S

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

A Bogus
A Bogus

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

Related Questions