简体   繁体   English

MVC 5和环境路由参数出现问题

[英]Trouble with MVC 5 and ambient route parameters

Link to my sample project 链接到我的示例项目

In the project above (MVC 5) I am having issues with Html.ActionLink seemingly not clearing out the routing parameters from the page its currently on. 在上面的项目(MVC 5)中,我遇到了Html.ActionLink问题,似乎没有从当前页面清除路由参数。

The exact same call to Html.ActionLink on one page produces different results depending on how "deep" into the route you are. 一页上对Html.ActionLink的完全相同的调用会产生不同的结果,具体取决于您对路线的“深度”。 Below is my Sample Controller 以下是我的示例控制器

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

Below is a snippet from the Sample/Index view 以下是“样本/索引”视图中的摘录

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

This produces the following HTML: http://localhost:2762/sample/example/100 这将产生以下HTML: http://localhost:2762/sample/example/100

Below is a snippet from Sample/Example view 以下是“示例/示例”视图中的摘录

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

<br />

@Html.ActionLink("Deeper Link", "Example", new { controller="Sample", parentcategoryid=9999, categorydepth=2})

The first time you get there by clicking the link from the Index view...the "Link" Html is the same: http://localhost:2762/sample/example/100 第一次通过单击“索引”视图中的链接到达那里时,“链接” HTML相同: http://localhost:2762/sample/example/100

If you click "Deeper Link", you get this same view again...however the "Link" Html is now: http://localhost:2762/sample/example/9999/2?categoryid=100 which clearly does not go where you'd like it to go. 如果单击“ Deeper Link”,您将再次获得相同的视图...但是,“ Link” HTML现在为: http://localhost:2762/sample/example/9999/2?categoryid=100显然不会显示您想去的地方。

This is basically a breadcrumb scenario for browsing product categories. 这基本上是浏览产品类别的面包屑场景。

Any thoughts on a clean and manageable fix for this? 关于此问题的干净且可管理的修复有什么想法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM