MVC 5和环境路由参数出现故障

本文关键字:故障 参数 路由 环境 MVC | 更新日期: 2023-09-27 18:14:33

链接到我的示例项目

在上面的项目(MVC 5(中,我遇到了Html.ActionLink的问题,它似乎没有从当前打开的页面中清除路由参数。

在一个页面上对Html.ActionLink进行完全相同的调用会产生不同的结果,这取决于您对路由的"深度"。下面是我的样品控制器

    // GET: Sample
    public ActionResult Index()
    {
        return View("Index");
    }
    [Route("sample/example/{categoryid}")]
    public ActionResult Example(int categoryID)
    {
        ViewBag.categoryID = categoryID;
        return View("Example");
    }
    [Route("sample/example/{parentcategoryid:int}/{categorydepth:int}")]
    public ActionResult Example(int parentcategoryID, int categorydepth)
    {
        ViewBag.parentcategoryID = parentcategoryID;
        ViewBag.categorydepth = categorydepth;
        return View("Example");
    }

以下是示例/索引视图的片段

@Html.ActionLink("Link", "Example", new { controller = "Sample", categoryid = 100 }, null)

这将生成以下HTML:http://localhost:2762/sample/example/100

以下是示例/示例视图的片段

    @Html.ActionLink("Link", "Example", new { controller = "Sample", categoryid = 100 }, null)
<br />
@Html.ActionLink("Deeper Link", "Example", new { controller="Sample", parentcategoryid=9999, categorydepth=2})

第一次通过单击"索引"视图中的链接到达那里时。。。"链接"Html与http://localhost:2762/sample/example/100 相同

如果你点击"深层链接",你会再次看到同样的视图。。。然而,"链接"Html现在是:http://localhost:2762/sample/example/9999/2?categoryid=100,它显然没有到达您想要的位置。

这基本上是一个浏览产品类别的面包屑场景。

有没有想过一个干净且易于管理的解决方案?

MVC 5和环境路由参数出现故障

在控制器中将Example((更改为Example2((