动作链接到“丢失”参数

时间:2012-06-19 07:46:02

标签: c# asp.net-mvc asp.net-mvc-routing actionlink

我有以下路线:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,"Conference/{shortName}/Submission/{submission}/{action}", new { controller = "Conference", action = "Show" });
routes.MapRoute(null,"Conference/{shortName}/{action}",new { controller = "Conference", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );

以下(希望是显而易见的)链接都在起作用:

  • /会议/ testconf
  • /会议/ testconf / ShowSubmissions
  • /会议/ testconf /提交/ firstSub
  • /会议/ testconf /提交/ firstSub / EditSubmission

当我现在在Submission / firstSub中并创建一个像这样的ActionLink

@Html.ActionLink("Show Submissions", "ShowSubmissions", "Conference", new { shortName = Model.confereceShortName },null)

它会创建以下链接

  • /会议/ testconf /提交/ firstSub / ShowSubmissions

我怎么能让actionlink忘记Submission / firstSub而不用硬编码呢?

2 个答案:

答案 0 :(得分:1)

您在哪里有{controller}的占位符?

默认路线应如下所示。

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{*id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
                );

还尝试删除第一条路线中的/ Submission / part。 关于自定义路线创建和订购的帖子的链接:

1)Creating Custom Routes (C#)

2)Custom routing for ASP .NET MVC

3)official source from asp.net mvc

答案 1 :(得分:1)

有时候搜索30分钟是不够的,你得搜索2小时......

ASP.NET MVC 2 RC2 Routing - How to clear low-level values when using ActionLink to refer to a higher level?

Routlink或删除约束中的值。 对我来说,Routelink完成了这项工作。

虽然谢谢

ElYusubov&阿列克谢