Question

Adding this for posterity since I couldn't find anything specific in my Googling endevors.

Problem:

The call is ambiguous between the following methods or properties: 'System.Web.HttpUtility.UrlEncode(string)' and 'System.Web.HttpUtility.UrlEncode(byte[])'

Note: this issue is specific to, at the very least, overloaded methods that accept a string in one signature and a byte[] in a similar signature (see UrlEncode signatures above).

This issue was presenting itself as a RuntimeBinderException in a partial using ASP.NET MVC4 but it could happen in other environments. The key is that UrlEncode has the two overloads and the runtime binder can't figure out which overload to use.

Was it helpful?

Solution

My Solution:

Although not optimal, casting the input as the correct type seems to give the runtime binder enough of a tip that it can resolve the correct method signature to use.

For example, here is some "failing" code that threw the above exception (from an MVC4 partial - code is from a contractor and I'm not proposing this is the best way to create an anchor tag - yet it does illustrate the specific issue nicely):

<a rel="nofollow"
   href="https://twitter.com/share?text=Check%20this%20out!%20@(ViewBag.Title)%20
   @(HttpUtility.UrlEncode(Request.Url.AbsoluteUri))%20%40codinghorror"
   target="_blank">
  twitter
</a>

And here it is, rewritten slightly with casting:

<a rel="nofollow"
   href="https://twitter.com/share?text=Check%20this%20out!%20@(ViewBag.Title)%20
   @(HttpUtility.UrlEncode((string)Request.Url.AbsoluteUri))%20%40codinghorror"
   target="_blank">
  twitter
</a>

After this fix, I reloaded the offending page and sure enough, POOF!, it worked.

Happy Trails!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top