如何生成链接时,忽略当前路径值? [英] How to ignore current route values when generating links?

查看:114
本文介绍了如何生成链接时,忽略当前路径值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是类似<一个href=\"http://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want\">asp.net MVC Html.ActionLink()保持路线值我不想,但与一捻,使得它更加复杂。

The question is similar to asp.net mvc Html.ActionLink() keeping route value I don't want, but with a twist that makes it more complex.

从默认的新MVC3应用程序开始,我改变路线:

Starting from a default new MVC3 app, I change the routes to:

routes.MapRoute(
    "r1", // Route name
    "{controller}/{id}/{action}"
);

routes.MapRoute(
    "r2", // Route name
    "{controller}/{action}"
);

请注意,该ID进来第一个动作之前。

Notice that the id comes before the action in the first.

然后在主页\\ Index.cshtml,我补充一下:

Then in Home\Index.cshtml, I add:

@Url.Action("Index")
@Url.Action("Index", new { id = "blah" })
@Url.Action("Index", new { id = "" })

现在我浏览到/ home /美孚/指数,并期待在3生成的链接。我得到

Now I navigate to /Home/Foo/Index and look at the 3 generated links. I get



  1. /首页/胡说/索引

  2. /首页/指数?ID =富

前两个有意义,并且使用的是第一条路。

The first two make sense, and are using the first route.

但在第三环节,它打第二条路线,我不明白为什么ID = foo是在查询字符串中传递,因为我明确地传递了一个空号。我希望它只是产生/首页/索引。

But in the third link, which hits the second route, I don't understand why id=Foo is passed on the query string, given that I explicitly passed an empty id. I would expect it to just generate "/Home/Index".

任何人都可以解释,并建议我怎么才能得到它不显示?

Can anyone explain that, and suggest how I can get it not to show up?

推荐答案

我只是测试这一点,它似乎工作正常。

I just tested this and it seems to work ok.

Url.Action("Index", new { id = UrlParameter.Optional })

生成

/Home/Index

这篇关于如何生成链接时,忽略当前路径值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆