简体   繁体   中英

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.

The exact same call to Html.ActionLink on one page produces different results depending on how "deep" into the route you are. 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

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

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.

This is basically a breadcrumb scenario for browsing product categories.

Any thoughts on a clean and manageable fix for this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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