3

I would like to create a dynamic routing to a URL like following:

http://localhost:51577/Item/AnyActionName/Id

Please note that the controller name is static and doesn't need to be dynamic. On the other hand, I need to have the action name part dynamic so that whatever is written in that part of URL, I would redirect the user to the Index action inside of Item controller.

What I have tried so far is:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "Items",
        "Item/{action}/{id}",
        new { controller = "Item", action = "Index",  id = UrlParameter.Optional });
}

And when I build my app I get a following error:

The resource cannot be found.

Edit:

Here is my Global.asax file and the routeconfig.cs file:

public class MvcApplication : System.Web.HttpApplication
{

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

And here's the content of the RouteConfig.cs file with the answer that @Nkosi provided:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
            name: "Items",
            url: "Item/{id}/{*slug}",
            defaults: new { controller = "Item", action = "Index", slug = UrlParameter.Optional }
        );
    }
}
9
  • so even though someone request item/create, it should go to Index action ? Commented Aug 27, 2016 at 20:11
  • Yeah but it's never gonna be item/create, it's gonna be a item's name :) Commented Aug 27, 2016 at 20:14
  • So are you saying {action} part of the url is the name of the item ? Do you have a parameter ? can you share the Index action method signature and the url you are trying to access ? Commented Aug 27, 2016 at 20:17
  • Yes I can share, the ID of the item is the parameter that I'm going to use to retrieve the data on the item, and the dynamic action name is just to make it more human friendly :) Commented Aug 27, 2016 at 20:23
  • 1
    Can you share the url you are trying and how you expect that to map to ? Commented Aug 27, 2016 at 20:31

1 Answer 1

4

What you are referring to in your question is called a slug.

I answered a similar question here for web api

Web api - how to route using slugs?

With the slug at the end the route config would look like this

public static void RegisterRoutes(RouteCollection routes) {

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Items",
        url: "Item/{id}/{*slug}",
        defaults: new { controller = "Item", action = "Index", slug = RouteParameter.Optional }
    );

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

}

which could match an example controller action...

public class ItemController : Controller {
    public ActionResult Index(int id, string slug = null) {
        //...
    }
}

the example URL...

"Item/31223512/Any-Item-Name"

would then have the parameters matched as follows...

  • id = 31223512
  • slug = "Any-Item-Name"

And because the slug is optional, the above URL will still be matched to

"Item/31223512"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.