Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - Dynamic routing action name in ASP.NET MVC

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 }
        );
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...