ASP.NET MVC Url.Action增加了当前路由值生成的URL [英] ASP.NET MVC Url.Action adds current route values to generated url

查看:205
本文介绍了ASP.NET MVC Url.Action增加了当前路由值生成的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题,一对夫妇在这里倍左右,但其中没有任何可以接受的答案:

I have seen this question a couple of times here in SO but none of them with any acceptable answer:

<一个href=\"http://stackoverflow.com/questions/6950470/asp-net-mvc-url-action-includes-current-route-data\">ASP.NET MVC @ Url.Action包括当前的路由数据结果
ASP.NET MVC隐式添加路由值

基本上我有控制器称为集团的操作方法,它具有接收任何参数,并显示元素的列表,并接收ID和细节显示该组另一个超载。

Basically I have Controller with an action method called Group, it has an overload that receives no parameters and displays a list of elements and another one that receives an id and displays details for that group.

如果我做这样的事情:

Url.Action("Group", "Groups");

从网站的主页(/)它返回一个URL是这样的:

From the main page of the site (/) it returns an url like this:

"mysite.com/Groups/Group"

这是正常的
现在,如果网站的当前地址是/组/组/ 1
我调用相同的方法

which is alright Now, if the current address of the site is /Groups/Group/1 And I call the same method

Url.Action("Group", "Groups");

返回的URL是这样的:

the returned url is this:

"mysite.com/Groups/Group/1"

它生成的URL时,自动添加路由的价值,为当前页面。
即使我生成URL是这样的:

It automatically adds the value of the route for the current page when generating the URL. Even if I generate the URL this way:

Url.Action("Group", "Groups", null);

因此​​明确指定我不希望任何路由值,生成的URL是一样的。
为了得到我想要我的路线值明确设置为空字符串,地址如下所示:

Thus explicitly specifying that I don't want any route values, the generated URL is the same. To get the address I want I have to explicitly set the route value to an empty string, like so:

Url.Action("Group", "Groups", new {id=""});

这将产生以下网址:

"mysite.com/Groups/Group"

我的问题是,为什么会出现这种情况?如果我没有设置任何路由值不应该将它们添加到生成的URL。

My question is, why does this happen? If I don't set any route values it shouldn't add them to the generated URL.

推荐答案

Url.Action将重用当前请求的参数,如果你不明确设置。它是由出站URL匹配算法设计。当寻找生成URL的过程中的路线数据参数,参数摘自:

Url.Action will reuse the current request parameters, if you do not explicitly set them. It is by design in outbound url-matching algorithm. When looking for the route data parameters in a process of generating url, parameters are taken from:

1)明确规定值

2)从当前请求值

3)默认

在我上面指定的顺序。

有关出境航线匹配算法复杂,所以它是明确设置请求的所有参数很好的做法,因为你在你的例子确实

Outbound matching algorithm for routes is complicated, so it is good practice to explicitly set all parameters for request, as you did in your example

这篇关于ASP.NET MVC Url.Action增加了当前路由值生成的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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