HtmlHelper.GenerateRouteLink没有给出预期的结果
问题描述:
我(拼命地)试图从我的一个控制器创建链接。HtmlHelper.GenerateRouteLink没有给出预期的结果
它应该看起来像这样:
http://localhost:50074/User/Profile?UserId=17&key=agasgqwefq323432r13dfd
相关的(尽管可能是错误的)路线:
routes.MapRoute(
"UsersRoute", // Route name
"{controller}/{action}/{userId}/{key}",
new { controller = "User", action = "Profile",userId=UrlParameter.Optional,key=UrlParameter.Optional });
和链接代码:
HtmlHelper.GenerateRouteLink(requestContext, RouteTable.Routes, "MyLinkText", "UsersRoute", new RouteValueDictionary(new { userId = userId, key = HashPassword(password) }), null);
(其中我送请求上下文:HttpContext.Request.RequestContext
。
目前这给了我:
<a href="http://Account/Register/29/E96303852080B94DC230611A3F4806" target="_blank">MyLinkText</a>
任何(来自我称之为GenerateRouteLink
是帐户/注册,和我不知道如何创建一个正确的,如果真正需要的情况下)帮助将不胜感激。
答
在你的路由定义的URL会的形式为:
{controller}/{action}/{userId}/{key}
,所以我不看你怎么可能指望形式的URL({controller}/{action}
)当您指定userid
和key
令牌:
/User/Profile?UserId=17&key=agasgqwefq323432r13dfd
为什么不简单地只保留默认的url定义?
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
然后,你可以通过简单地获得所需形式的URL:
@Html.ActionLink(
"MyLinkText",
"Profile",
"User",
new { userId = userId, key = HashPassword(password) },
null
)
感谢您选择的答案,但生成的链接是一样的.. –
我知道该怎么做,从一个视图,问题是我不能这样做从CS ..(即控制器) –
@Oren A,在控制器中,您可以使用'Url.Action'方法。 –