Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to URL encode parameters in ASP .NET MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...