-2

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.

longda
  • 10,153
  • 7
  • 46
  • 66

1 Answers1

0

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!

longda
  • 10,153
  • 7
  • 46
  • 66
  • 1
    Return type is not part of the method signature, I don't understand how it fixed your problem. http://stackoverflow.com/questions/8808703/method-signature-in-c-sharp – I4V May 13 '13 at 20:42
  • Hi, not talking about return type. I'm talking about the various inputs for overloads. Here is the current list of overloads for UrlEncode(): http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx – longda May 13 '13 at 20:49
  • longda, You don't have to post a link for me, since, after your edit, your answer changed completely (If you think the effect of the change, it is much more then *typo*) . Your unedited answer were actually *talking about return type* – I4V May 13 '13 at 20:57
  • 1
    @I4V - Thanks for the clarification. I'll leave the link in since it helps explain the issue a bit. Thanks for the catch! – longda May 13 '13 at 21:24