2

I have a deep-linking Silverlight RIA trying to consume a Twitter OAuth callback. The URL of the callback "page" in the RIA is:

http://example.com/RiaTestPage.aspx#callback

Twitter calls back to this URL so long as the # sign is URL encoded; so the callback url I supply to Twitter is:

http://example.com/RiaTestPage.aspx%23callback

RiaTestPage.aspx does of course exist, but when Twitter calls back to this URL, I'm getting a 404 (from the VS 2010 ASP.NET Development server)

Server Error in '/' Application

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /RiaTestPage.aspx#callback

Although the # sign has been correctly decoded in the error message above, the 404 seems to be the result of the encoded # sign. If I manually change the callback url that caused the 404,

http://example.com/RiaTestPage.aspx%23callback

to this:

http://example.com/RiaTestPage.aspx#callback

The callback page in the RIA loads normally. Why am I receiving a 404 in this situation?

Community
  • 1
  • 1
with
  • 306
  • 3
  • 15
  • It's worth mentioning that other OAuth services (besides Twitters) do sometimes support the # addressing, and it works perfectly fine. So it's not fundamentally impossible to do this, it's just a matter of Twitter supporting it. I've got a ticket in with Twitter to see if they can add it. – with Sep 24 '10 at 17:48

3 Answers3

5

You get a 404 error because the #callback part is not part of the URL. It's a bookmark that is used by the browser, and it's never sent in the request to the server. If you encode the hash, it becomes part of the file name instead, and there is no file named RiaTestPage.aspx#callback.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    It is possible to send a # in the request to the server. But you're right about the encoded hash becoming part of the file name, that's what caused the 404. – with Sep 24 '10 at 17:50
  • I think the real question for the OP is whether or not they can just call `http://example.com/RiaTestPage.aspx#callback` instead of the encoded uri. Near as I can tell, that would fix their problem. – Patrick M Jul 20 '12 at 23:56
  • @PatrickM: I thought that would be obvious from the answer. If you encode the hash it becomes part of the page name, thus if you don't encode the hash it doesn't become part of the page name. – Guffa Jul 21 '12 at 04:14
  • @Guffa: I should probably delete my comment; I thought the OP was asking 'how to fix it' not 'why is it this way.' Sorry, I sounded like a jerk. – Patrick M Jul 21 '12 at 16:07
2

Cause of the problem explained by Guffa already. Several solutions exist though:

1. Redirection service:

Use a redirection service (like bit.ly, or even your own server) for the URL you publish. It will provide a standard link that Twitter will allow and send it to the correct page on your site. These services can also make the URL much shorter, which is a plus for Twitter posts.

2. Custom 404:

Implement a custom 404 page on your site and when specific URLs, like your example come in, correct the encoding and redirect to your correct page.

3. Simply add a ? to your link!:

The ? character marks the end of the file specification and the start of parameters. Parameters are not actually required before a bookmark so this URL should work for a Twitter link:

http://example.com/RiaTestPage.aspx?%23callback

will call

http://example.com/RiaTestPage.aspx?#callback

which is the same as:

http://example.com/RiaTestPage.aspx#callback
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
1

In IIS7 you can use the URL rewrite module or manually edit the web.config file to add this rewrite rule which turns %23 into #. It doesn't look intuitive because you put the # in both the regex find and replace boxes, but it works. This redirects incoming URLs like www.domain.com/page%23anchorname to www.domain.com/page#anchorname, and avoids the 404 error.

<rewrite>
  <rules>
     <rule name="unencode hashmark">
        <match url="^([^\#]*)#(.*)$" />
        <action type="Redirect" url="{R:1}#{R:2}" />
     </rule>
  </rules>
</rewrite>