8

I have the following code in my view:

<%= Html.ActionLink(
           "View item", 
           "Index", 
            "Items", 
            new 
            { 
                itemName = Model.ItemName 
            }, 
            null) %>

I have a problem when the item name contains a sharp (#) or the percent symbol (%).

  • When the item name is "name#with#sharp#", the controller receives only the first part of the name until the first sharp (only receives "name").

  • When the item name is "name%with%percent" I get an error: HTTP error 400 - Bad request.

I not sure if this is a problem with the URL encoding, because it works with other conflictive chars such as:

;
=
+
,
~
[blank]

Do you know how could I address this issue?

Thanks in advance.

1
  • What urls are getting generated? When I try your example above with the '%' symbols, I get this: http://localhost/AspNetMvc2/Items?itemName=name%25with%25percent, and no errors. Also, the example with the # symbol produces this: http://localhost:55386/Items?itemName=name%23with%23sharp%23
    – Jeff Ogata
    May 5, 2011 at 17:25

3 Answers 3

11

I'm assuming you have a route setup and your url looks something like this:

http://localhost/Items/Index/name%25with%25percent - (this will blow up)

as opposed to this:

http://localhost/Items/Index/?itemName=name%25with%25percent - (query string is ok)

So an option would be to remove the "itemName" property from your route (in your RouteCollection) so that Html.ActionLink will render the Url using itemName as a QueryString parameter.

As @Priyank says, the problem is because the itemName is part of the Url (not a QueryString parameter) and it contain illegal characters.

1
  • Yes, you're assuming right. So the only place to use % symbol is a query string. Is there any other way to use % in the URL? May 6, 2011 at 8:32
4

Since these routedvalues are posted as part of URL string, they will be treated as separate values, separated by # and %. There are couple options for handle your case.

You will have to implement your custom ValueProvider (IValueProvider and especially RouteDataValueProvider) to handle your custom need. One programmer had an issue with character '/' and he hacked it here http://mrpmorris.blogspot.com/2012/08/asp-mvc-encoding-route-values.html

Second is to store values in TempData which persists across two request and use them.

Hope this helps to think in right direction.

0

You should be able to just use the UrlHelper instance of your view to do this for you. Try giving this a shot:

<%= Html.ActionLink( "View item", "Index", "Items", new { itemName = Url.Encode(Model.ItemName) }, null) %>

Update

After testing, it seems explicitly encoding like I did above seems to be less accurate and will cause the server to double-encode (eg - % will come out as %2525 in the url).

4
  • 1
    This is not enough. IIS is converting name+hello into name%25%2bhello that fails with a bad request May 5, 2011 at 17:01
  • Comes out fine for me whether I encode or not. 200 status code across the board. It actually seems most accurate when I don't encode, and just let routing and IIS do their thing normally. Just tested in Casini and IIS Express with your url with sharps (#), percents (%), and plusses (+) examples. What version of IIS are you using? May 5, 2011 at 17:42
  • 1
    to reproduce the problem you will have to setup a route with a token called itemName - so the url looks something like: localhost/Items/Index/name%25with%25percent - this url contains illegal characters
    – Lee Gunn
    May 5, 2011 at 19:48
  • @LeeGunn Thanks for the tip. Seems in my tests the url with sharps (#) still works fine w/ the route token defined. The percent (%) triggers asp.net request validation, and the plusses (+) returns a 404. I tried the fix @Priyank suggested and that seemed to work. Looks like he's got the answer. If I had the rep to, I'd upvote him. :) May 5, 2011 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.