71492

Url encoded # (%23) causing 404 in ASP.NET application

Question:

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)

<blockquote>

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

</blockquote>

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?

Answer1:

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.

Answer2:

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

<h2>1. Redirection service:</h2>

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. <strong>These services can also make the URL much shorter, which is a plus for Twitter posts</strong>.

<h2>2. Custom 404:</h2>

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.

<h2>3. <em>Simply add a ? to your link</em>!:</h2>

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

Answer3:

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>

Recommend

  • GET curl call not returning JSON object
  • Express.js ERR_TOO_MANY_REDIRECTS error
  • How to consume and replace a value in an &mut ref [duplicate]
  • Easily override NuGet DLL in development (VS 2015)
  • GitLab site is showing the plesk default page. What am i doing wrong?
  • How do ES6 tagged template functions interpret their argument order?
  • wp-admin can't access after moving
  • Is there a way to selectively skip silverlight field validation when using wcf ria?
  • Alternative to require.paths to edit require search directories programmatically?
  • SQL Service Broker: Collecting data from distributed sources.
  • Convert date string value from U.S. to native Date with current locale U.S.?
  • IIS 7.5 404 Error for .PDF files
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • What is the best way to debug Bootstrap.groovy?
  • What is this strange character in chrome's resource css viewer?
  • Most efficient way to move table rows from one table to another
  • Can't remove headers after they are sent
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • how does System.Web.HttpRequest::PathInfo work?
  • Retrieve list of sent friend requests from friend_request FQL table
  • Test if a set exists before trying to drop it
  • Chrome doesn't support silverlight anymore? How to solve this?
  • NHibernate Validation Localization with S#arp Architecture
  • Highlight one bar in a series in highcharts?
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • Calling of Constructors in a Java
  • php design question - will a Helper help here?
  • Is there a mandatory requirement to switch app.yaml?
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • AngularJs get employee from factory
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass