传递带有空格的querystring? [英] Passing in a querystring with a space?
问题描述
我有一个这样的动作:
public ActionResult UserDetails(string id)
{
}
context.MapRoute(
"Admin_Default",
"{controller}/{action}/{id}",
new { action = "Index", controller = "Start", id = UrlParameter.Optional }
);
在另一个视图中,我有一个来自.net中标准成员资格管理的用户的列表想法是您单击一个用户名以显示详细信息,并且仅当该用户名用一个单词表示时,此方法才有效.我最近遇到了一个看起来像这样的用户名:
In another view i have a list with users from the standard membership management in .net Idea is that you click on a username to show the details and this works, only if the username is in one word. I recently encountered a username that looks like this:
Steven C.H.安德森(Andersson)
Steven C.H. Andersson
所以我用它来产生链接:
So i use this to produce the link:
@Html.ActionLink(Model.UserName, "UserDetails", new { controller = "Security", id = HttpUtility.UrlEncode(Model.UserName) })
但是当我单击它时,我得到:HTTP错误404.11-找不到
But when i click on it, i get: HTTP Error 404.11 - Not Found
如果我单击一个单词的普通用户名,它就像一个超级按钮.
If i click on a normal username that is one word, it works like a charm.
我认为路由配置不了解此请求,问题是如何使其工作?
I assume that the route config is not understanding this request, question is how to get it to work?
更新
链接:
<a href="/Security/UserDetails/Steven%2bC.H.%2bAndersson">Steven C.H. Andersson</a>
推荐答案
我遇到了同样的问题,这是我解决的方法,但是我不确定这是最好的方法.我渴望听到一个更优雅的解决方案.
I had the same problem, here is how I solved it, but I am not sure it is the best way. I am keen to hear a more elegant solution.
在视图文件中:
@Html.ActionLink(Model.UserName, "UserDetails", new { controller = "Security", id = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Model.UserName)) })
在控制器文件中:
id = Encoding.UTF8.GetString(Convert.FromBase64String(id))
这篇关于传递带有空格的querystring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!