0

I have an MVC 5 site with this structure:

example.com
example.com/area1 (index for whole section)
example.com/area2/item5 (specific item for section)

For SEO reasons I'd like to change every URL to this:

example.com/keyword (redirected from example.com)
example.com/keyword/area1
example.com/keyword/area2/item5

The keyword is fixed and always the same, though at some point in the future there may be a site with the same structure but different content with a different keyword in. That is probably at least a few months off though.

What is the quickest / easiest way to implement the above. Being able to get the name of keyword would be an advantage later though isn't essential right now.

I could use attribute routing, though I'd have to update A LOT of ActionMethods to do this - and most currently don't even use attribute routing.

thx.

Update

I've tried adding the below to RouteConfig.cs, but none of the Url's work for some reason:

routes.MapRoute(
    name: "Default",
    url: "keyword/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
niico
  • 11,206
  • 23
  • 78
  • 161

1 Answers1

0
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    string  keyword = "keyword";
    // keyword route - it will be used as a route of the first priority 
    routes.MapRoute(
        name: "Defaultkeyword",
        url: "{keyword}/{controller}/{action}/{id}",
        defaults: new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional,
            keyword = keyword
        });

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

also you need to add this Defaultkeyword route with few changes into your Area1AreaRegistration.cs file

public override void RegisterArea(AreaRegistrationContext context)
 {
     context.MapRoute(
            "Area1_defaultKeyword",
            "{Keyword}/Area1/{controller}/{action}/{id}",
            new { Keyword = "Keyword", controller = "HomeArea1", action = "Index", id = UrlParameter.Optional }
            );

 context.MapRoute(
            "Area1_default",
            "Area1/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
           );
 }
Mannan Bahelim
  • 1,289
  • 1
  • 11
  • 31