UrlHelper.Action包括不需要的附加参数 [英] UrlHelper.Action includes undesired additional parameters
问题描述
我在控制器的方法 ApplicationsController
,其中我需要得到基本URL的操作方法:
I have a method in the controller ApplicationsController
, in which I need to get the base URL for an action method:
public ActionResult MyAction(string id)
{
var url = Url.Action("MyAction", "Applications");
...
}
问题是,这包括字符串ID
从目前的路由数据,当我需要的URL,而不(URL被用来在一个从CMS获取内容基于URL的查找)。
The problem is that this includes the string id
from the current route data, when I need the URL without (the URL is used to fetch content from a CMS on a URL-based lookup).
我曾尝试通过空
和新{}
为 routeValues
参数都无济于事。
I have tried passing null
and new { }
as the routeValues
parameter to no avail.
匹配的路线如下(所有其他路线以上):
The matching route is as follows (above all other routes):
routes.MapLowercaseRoute(
name: "Applications",
url: "applications/{action}/{id}",
defaults: new { controller = "Applications",
action = "Index", id = UrlParameter.Optional });
我见过一对夫妇的其他问题触及到这一点,但他们都不似乎有一个可行的解决方案。在present,我诉诸硬编码在控制器的路径;不过,我希望能够抽象到这一行动过滤器,所以我需要能够生成的URL。
I've seen a couple of other questions touch on this but none of them seem to have a viable solution. At present, I am resorting to hardcoding the path in the controller; however, I'd like to be able to abstract this into an action filter, so I need to be able to generate the URL.
有没有prevent清洁/传统的方式这种行为?
Is there a clean/conventional way to prevent this behaviour?
推荐答案
最终得到解决此用不同的方法。我可以拿出来prevent随意命名路由值被插入到生成的URL的唯一办法是从的RouteData code>暂时删除它们时调用
Url.Action
。我写了一对夫妇的扩展方法以促进这一:
Ended up getting around this with a different approach. The only way I could come up with to prevent arbitrarily-named route values from being inserted into the generated URL was to temporarily remove them from RouteData
when calling Url.Action
. I've written a couple of extension methods to facilitate this:
public static string NonContextualAction(this UrlHelper helper, string action)
{
return helper.NonContextualAction(action,
helper.RequestContext.RouteData.Values["controller"].ToString());
}
public static string NonContextualAction(this UrlHelper helper, string action,
string controller)
{
var routeValues = helper.RequestContext.RouteData.Values;
var routeValueKeys = routeValues.Keys.Where(o => o != "controller"
&& o != "action").ToList();
// Temporarily remove routevalues
var oldRouteValues = new Dictionary<string, object>();
foreach (var key in routeValueKeys)
{
oldRouteValues[key] = routeValues[key];
routeValues.Remove(key);
}
// Generate URL
string url = helper.Action(routeValues["Action"].ToString(),
routeValues["Controller"].ToString());
// Reinsert routevalues
foreach (var kvp in oldRouteValues)
{
routeValues.Add(kvp.Key, kvp.Value);
}
return url;
}
这允许我做这一个动作过滤器,我不一定会知道的参数名称为行动的(因此不能只传递一个匿名对象在其他的答案)。
This allows me to do this in an action filter where I won't necessarily know what the parameter names for the action are (and therefore can't just pass an anonymous object as in the other answers).
还是非常有兴趣知道,如果有人有更好的解决方案,但是。
Still very much interested to know if someone has a more elegant solution, however.
这篇关于UrlHelper.Action包括不需要的附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!