【问题标题】:MVC Routing get rid of /Index in URLMVC 路由摆脱 URL 中的 /Index
【发布时间】:2012-11-26 15:39:09
【问题描述】:

我有一个在运行时生成的动态链接列表,如下所示:

@Html.ActionLink(x.Name, "Index", "User", new { x.ID }, null)

所以我在用户控制器上点击 Index 方法。

我的 RouteConfig.cs(它是一个 MVC4 应用程序)也设置如下:

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

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

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

这给了我链接列表中的链接server/application/User/Index/1

我需要在列表生成和/或路由文件中做什么才能将其更改为server/application/User/1

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing optional-parameters


    【解决方案1】:

    添加不需要操作但不会与其他路由冲突的路由

    routes.MapRoute("User", "User/{id}", 
                             new { controller = "User", action = "Index" });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-07-20
      • 1970-01-01
      • 2021-10-13
      相关资源
      最近更新 更多