C# MVC RouteConfig影响了Html.ActionLink()助手方法

C# MVC RouteConfig影响了Html.ActionLink()助手方法,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc Routing,在我的MVC项目中,我有项控制器和一些操作,如索引 RouteConfig包括: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 在一些视图中

在我的MVC项目中,我有控制器和一些操作,如索引

RouteConfig包括:

  routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
在一些视图中,我使用helper方法Html.ActionLink(“Items”、“Index”、“Item”)为索引操作创建锚定。因此锚定结果的href将是(/Item/Index

现在,我需要映射以下静态URL:

/间接项/索引

使用默认参数(indirect=true)对项的操作执行索引,因此RouteConfig将为:

 routes.MapRoute(
                name: "IndirectItem",
                url: "IndirectItem/Index",
                defaults: new { controller = "Item", action = "Index", indirect = true  }
            );
   routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
看起来还可以,客户端请求映射正确,但是所有由Html.ActionLink(“Items”、“Index”、“Item”)助手方法生成的锚都映射到了URL(/IndirectItem/Index),而不是(/Item/Index


如何在不将所有Html.ActionLink()更改为Html.RouteLink()的情况下解决此问题或为原始url添加另一个路由?

您遇到此问题是因为
Html。ActionLink
使用路由表生成url,并且由于
间接项
路由与
Html匹配。ActionLink(“项目”、“索引”、“项目”)
(因为它在路由和操作链接中都指定了索引操作和项目控制器)。解析由第一个匹配项完成,因此路由注册的顺序很重要

通过添加
DefaultItem
route:

routes.MapRoute(
     name: "DefaultItem",
     url: "Item/Index/{id}",
     defaults: new { controller = "Item", action = "Index", id = UrlParameter.Optional  }
);
在当前路线之前:

routes.MapRoute(
       name: "IndirectItem",
       url: "IndirectItem/Index/,
       defaults: new { controller = "Item", action = "Index", indirect = true}
);
routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我们应该解决这个问题

另一个选项可以是创建从项控制器继承的空间接项控制器:

public IndirectItemController : ItemController
{
}
然后把路线改成

routes.MapRoute(
       name: "IndirectItem",
       url: "IndirectItem/Index/,
       defaults: new { controller = "IndirectItem", action = "Index", indirect = true}
); 

使用约束将是解决问题的方便方法

使用下面的间接项目路线代替您的路线

routes.MapRoute(
           name: "IndirectItem",
           url: "{staticVar}/{action}",
           defaults: new { controller = "Item", action = "Index", indirect = true},
           constraints: new { staticVar = "IndirectItem" }
        );
您不需要对默认路径进行任何更改


这对我来说很好。

奥马尔·戈哈尔和亚历克斯·阿特给出的答案具有误导性

您遇到的问题是,在生成URL时,您的路由不匹配。这只是因为您没有提供所有路由值,以便在
ActionLink
中创建匹配

@Html.ActionLink("Items", "Index", "Item", new { indirect = true }, null)
如果不能更改
ActionLink
声明,则可以使用将
的“间接”
元数据附加到路由

您可以使用DataTokens属性检索或分配与路由相关联的值,这些值不用于确定路由是否与URL模式匹配。这些值将传递给路由处理程序,可用于处理请求

底线是,
routeValue
(如果URL模式未提供,则由默认值填充)不用于元数据。它们是要匹配的真实数据,以使URL唯一

当然,如果实际上没有对任何内容使用
间接
路由值,则只需将其从路由中忽略即可

routes.MapRoute(
    name: "IndirectItem",
    url: "IndirectItem/Index",
    defaults: new { controller = "Item", action = "Index" }
);

我已经尝试了第一个解决方案,但我发现更改的效果会很好,因为相同的场景会应用于多个控制器。我认为第二个解决方案很好。谢谢。非常好的解决方案。这是一个动态路线,可以应用于多个操作。谢谢。
routes.MapRoute(
    name: "IndirectItem",
    url: "IndirectItem/Index",
    defaults: new { controller = "Item", action = "Index" }
);