4

I need help crating a permalink like URL routing in a MVC website.

The Slugs are already set up as www.xyz.com/profile/{slug}: the code is:

routes.MapRoute(
    name: "Profile",
    url: "profile/{slug}",
    defaults: new { controller = "ctrlName", action = "actionName" }
            );

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

What i need to achieve is a URL that you see on Wordpress permalinks or Umbraco permalinks. I need to have www.xyz.com/{Slug}.

I have tried to use:

 routes.MapRoute(
     name: "Profile",
     url: "{slug}",
     defaults: new { controller = "ctrlName", action = "actionName" }
            );

But that did not work for me.

EDIT:

If I switch the route configs above, the slug functionality works but the regular routing no longer does.

Does that mean that I am forced to implement permalink functionality on all pages?

M O H
  • 284
  • 3
  • 15
  • Do you have a controller named "ctrlName" with an action method named "actionName"? If yes the action should have a string parameter named "slug". if No, set the correct controller and action name in your route configuration. – Jimmy Hannon Jun 21 '15 at 13:30
  • I have the ctrlName and actionName set up with the slug as a string parameter. Like I said /profile/{Slug} is working. But /{Slug} is not. /{Slug} is for the same ctrlName actionName. – M O H Jun 22 '15 at 06:43
  • By the way, the exception is : HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. – M O H Jun 22 '15 at 06:52
  • 1
    What if you put your "Profile" route (the one with "/{slug}) after the default route and you remove the default controller and action from the default route. This way, the default route still works with for example url '/Home/Show' and the profile route should match all '/xyz' url's. – Jimmy Hannon Jun 22 '15 at 19:59

1 Answers1

4

If you want to have permalinks picked up from the root (site.com/{slug}) then you can use your slug route as is. But for any other controllers/actions work you'll need to explicitly specify a route for them ABOVE your slug route. Eg:

routes.MapRoute(
    name: "Services",
    url: "Services/{permalink}/",
    defaults: new { controller = "Page", action = "Services"}
);
routes.MapRoute(
    name: "Requests",
    url: "Requests/{action}/{id}",
    defaults: new { controller = "Requests", action = "Index", area = "" },
    namespaces: new String() {"ProjectNamespace.Controllers"}
);
routes.MapRoute(
    name: "AdminPreferences",
    url: "Admin/Preferences",
    defaults: new { controller = "Preferences", action = "Index", area = "Admin"},
    namespaces: new String() {"ProjectNamespace.Areas.Admin.Controllers"}
);
...
routes.MapRoute(
    name: "Profile",
    url: "{slug}",
    defaults: new { controller = "ctrlName", action = "actionName" }
);

This should work; I've acomplished this before but I'm afraid I'm answering from memory and VB. I converted the code from VB to C# in this text editor, so I can't be sure there isn't an error.

Matthew Hudson
  • 1,306
  • 15
  • 36
  • Thank you for this answer. I have fixed this already but your answer should definitely work. Thanks – M O H Jul 29 '15 at 09:04