Dynamic routing action name in ASP.NET MVC

早过忘川 提交于 2019-12-29 07:18:07

问题


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 }
        );
    }
}

回答1:


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"


来源:https://stackoverflow.com/questions/39185278/dynamic-routing-action-name-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!