开发者

Preventing route values from being used by ActionLink

This is effectively a duplicate of How Can I Stop ASP.Net MVC Html.ActionLink From Using Existing Route Values?

According to @Eilon -

Because once you invalidate a parameter segment then all parameter segments after it will get invalidated.

In my case, I have two Action links that link to actions on separate controllers.

Both actions have an optional page parameter.

The route configurations look like:

    // blog routes

    routes.MapRoute(
        "",
        "blog/page{page}",
        new { controller = "Blog", action = "Index" },
        new { page = @"\d+" }
    );

    routes.MapRoute(
        "",
        "blog/{id}",
        new { controller = "Blog", action = "Post" }
    );

    routes.MapRoute(
        "",
        "blog",
        new { controller = "Blog", action = "Index" }
    );

    // project routes

    routes.MapRo开发者_开发百科ute(
        "",
        "projects/page{page}",
        new { controller = "Project", action = "Index" },
        new { page = @"\d+" }
    );

    routes.MapRoute(
        "",
        "projects/{id}",
        new { controller = "Project", action = "Project" }
    );

    routes.MapRoute(
        "",
        "projects",
        new { controller = "Project", action = "Index" }
    );

The problem is that if I navigate to page 2 of projects, and click the blog link, I will be directed to /blog/page2.

Now according to the above, if a parameter segment is invalidated (in this case both the controller and action are different) then the page parameter should be invalidated. Does this work if the controller and action are specified using the standard ActionLink overloads, or does it only apply if they are passed in via the RouteValueDictionary?

Currently I have had to create a custom ActionLink to remove the "page" parameter from RouteData.

Thanks, Ben


This maybe? How to prevent Url.RouteUrl(...) from inheriting route values from the current request


Probably not the ideal. But you could: Html.ActionLink("action", "controller", new { page = "" })

Seems a bit better than creating a custom helper at least. HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜