开发者

HtmlHelper.GenerateRouteLink doesn't give expected result

I'm (desperately) trying to create a link from one of my controllers.

It should look like so:

http://localhost:50074/User/Profile?UserId=17&key=agasgqwefq323432r13dfd

The relevant (though probably erroneous) route:

routes.MapRoute(
             "UsersRoute", // Route name
            "{controller}/{action}/{userId}/{key}",
             new { controller = "User", action = "Profile",userId=UrlParameter.Optional,key=UrlParameter.Optional });  

And the link code:

HtmlHelper.GenerateRouteLink(requestContext, RouteT开发者_Go百科able.Routes, "MyLinkText", "UsersRoute", new RouteValueDictionary(new { userId = userId, key = HashPassword(password) } ), null);           

(where I send the request context by: HttpContext.Request.RequestContext.

This currently gives me:

<a href="http://Account/Register/29/E96303852080B94DC230611A3F4806" target="_blank">MyLinkText</a>

(the context from which I call GenerateRouteLink is Account/Register, and I don't know how to create a correct one, if that's really needed)

Any help will be much appreciated.


In your route definition the url will be of the form:

{controller}/{action}/{userId}/{key}

so I don't see how can you possibly expect an url of the form ({controller}/{action}) when you specify the userid and key tokens:

/User/Profile?UserId=17&key=agasgqwefq323432r13dfd

Why don't you simply leave only the default url definition?

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Then you could get urls of the desired form by simply:

@Html.ActionLink(
    "MyLinkText", 
    "Profile", 
    "User", 
    new { userId = userId, key = HashPassword(password) }, 
    null
)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜