Search code examples
c#query-stringresponse.redirectserver.transfer

Pass data to a URL on a different web server without the QueryString


I've got an .ashx handler which, upon finishing processing will redirect to a success or error page, based on how the processing went. The handler is in my site, but the success or error pages might not be (this is something the user can configure).

Is there any way that I can pass the error details to the error page without putting it in the query string?

I've tried:

  1. Adding a custom header that contains the error details, but since I'm using a Response.Redirect, the headers get cleared
  2. Using Server.Transfer, instead of Response.Redirect, but this will not work for URLs not in my site

I know that I can pass data in the query string, but in some cases the data I need to pass might be too long for the query string. Do I have any other options?


Solution

  • Essentially, no. The only way to pass additional data in a GET request (i.e. a redirect) is to pass it in the query string.

    The important thing to realise is that this is not a limitation of WebForms, this is just how HTTP works. If you're redirecting to another page that's outside of your site (and thus don't have the option of cookies/session data), you're going to have to send information directly in the request and that means using a query string.

    Things like Server.Transfer and Response.Redirect are just abstractions over a simple HTTP request; no framework feature can defy how HTTP actually works.

    You do, of course, have all kinds of options as to what you pass in the query string, but you're going to have to pass something. If you really want to shorten the URL, maybe you can pass an error code and expose an API that will let the receiving page fetch further information:

    1. Store transaction information (or detailed error messages) in a database with an ID.
    2. Pass the ID in the query string.
    3. Expose a web method or similar API to allow the receiving page to request additional information.

    There are plenty of hacky ways you could create the illusion of passing data in a redirect outside of a form post (such as returning a page containing a form and Javascript to immediately do a cross-domain form post) but the query string is the proper way of passing data in a GET request, so why try to hack around it?