Hyperlink '?' Escaping
I have a hyperlink with a queryparam in the following format:
<a href="<%= Url.Content("~/Search/Results/" +
ViewData["SearchID"] + "?listingID=" +
ViewData.Model.ListingID) %>">
See more
</a>
Shows up fine in the source of the page however when clicked I end up with:
http://localhost/{Site}/Search/Results/30%3FlistingID=2
I've tried using the ActionLink helper:
<%= Html.ActionLink("See more", "Results", "Search",
new {
id = ViewData["SearchID"],
listingID = ViewData.Model.ListingID
}, null) %>
I've even tried registering a route specific to this action:
routes.MapRoute(
"SearchResults", // Route name
"Search/Results/{id}/{listingID}", // URL with parameters
new { controller = "Search",
action = "Results",开发者_运维问答
id = UrlParameter.Optional,
listingID = UrlParameter.Optional } // Parameter defaults
);
I'm really baffled as to why the url shows up fine in the generated page source but not in the actual request url once clicked on. Any input?
Edit The url shows up properly in the generated page's source:
<a href="/[SITE]/Search/Results/30?listingID=2">See more</a>
Entering the above url manually into the address bar works properly oddly enough. Really baffled.
Second EDIT
The real reason that url thing is a problem is that I'm getting an error:
A potentially dangerous Request.Path value was detected from the client (?).
Final Edit
I found the cause of the problem. The request was coming from https to http and there is an attribute on the target Controller that rewrites the url to be http. /FacePalm
UrlHelper.Content is assuming everything is a protocol, host, and path, it really doesn't do query variables.
UriBuilder might help.
The url is turning your "?" into the uri encoded value: %3F. The reason that this is happening is because the string is being uri encoded. You need to escape the question mark.
精彩评论