77357

Passing in a querystring with a space?

Question:

I have an action like this:

public ActionResult UserDetails(string id) { } context.MapRoute( "Admin_Default", "{controller}/{action}/{id}", new { action = "Index", controller = "Start", id = UrlParameter.Optional } );

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

So i use this to produce the link:

@Html.ActionLink(Model.UserName, "UserDetails", new { controller = "Security", id = HttpUtility.UrlEncode(Model.UserName) })

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?

<strong>Update</strong>

Link:

<a href="/Security/UserDetails/Steven%2bC.H.%2bAndersson">Steven C.H. Andersson</a>

Answer1:

The encoding is obviously wrong, as it encodes spaces as "%2b", which is "+". You're passing not a query string argument, but a part of path, so you need to use HttpUtility.UrlPathEncode()

Answer2:

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.

In the view file:

@Html.ActionLink(Model.UserName, "UserDetails", new { controller = "Security", id = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Model.UserName)) })

In the controller file:

id = Encoding.UTF8.GetString(Convert.FromBase64String(id))

Recommend

  • LLDB: Must I build it from source (XCode project) to use it from the command line?
  • ASP.NET MVC: Restricting access using url
  • SQL: Query for a group that contains an exact set of users
  • SQL query comparing an attribute in multiple tuples based on values of another attribute within the
  • Alternative to XInclude
  • Connect to OLEDB data source from Java?
  • How to notify a specific thread in Java
  • Cannot run the macro
  • Eclipse terminate shotcut
  • Push Changes to Git not Functioning
  • getting domain details and portrait_id and landscape_id for openX
  • What is wrong in my MVC implementation?
  • SSRS 2008 - Sorting within a group
  • Where these are stored?
  • jQuery: How to AJAXify WordPress Search?
  • Hibernate to update table schema
  • Multiple producers single consumer locking schema
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Access Android Market through SSH tunnel
  • Declaring variable dynamically in VB.net
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • ImageMagick, replace semi-transparent white with opaque white
  • Master page gives error
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Join two tables and save into third-sql
  • Large data - storage and query
  • jquery mobile loadPage not working
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • Numpy divide by zero. Why?
  • Data Validation Drop Down Box Arrow Disappearing
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Load html files in TinyMce
  • Getting error when using KSoap library to consume .NET web services
  • How do you join a server to an Active Directory (domain)?