locked
Redirecting from an ashx RRS feed

  • Question

  • User-284642143 posted
    I have an ASHX handler which checks for a Post request, I'm reading the data into an input stream

    if(context.Request.HttpMethod.ToUpper() == "POST")
    {
    string json = new StreamReader(Request.InputStream).ReadToEnd();
    }

    Within this if block once I've processed the data I redirect using

    context.Response.Redirect("/page.aspx");

    This never works. After some digging around it seems this issue occurs only when it's s post. If I put the redirect outside of the block above then the redirect works but if course this means I can't process the data.

    How could I overcome this and redirect the Handler?
    Tuesday, December 17, 2019 6:21 PM

All replies

  • User475983607 posted

    What does "never works" mean?  How does your application work?

    Tuesday, December 17, 2019 6:35 PM
  • User-284642143 posted
    By that I mean the redirect never happens.

    So I receive some JSON data in the POST body request from a third party, which is then processed i.e. save the data to our database. This successfully completes.

    Once done I want to redirect the user from the ashx handler to a "complete" page. It's at this stage it never redirects.

    What I noticed is that if the Handler is hit without going into the IF block (as above) the redirect works (I just added the redirect code before everything else). Once it completes saving the data inside the post it doesn't redirect. Hope this makes sense?
    Tuesday, December 17, 2019 6:48 PM
  • User475983607 posted

    EssCee

    Once done I want to redirect the user from the ashx handler to a "complete" page. It's at this stage it never redirects.

    This design will not work.  Only the current request is redirected which is either the 3rd party or the user.  The 3rd party request cannot redirect the user.  You need to write code that checks a database when the user makes a request.  Then do something depending on what's in the database.

    Tuesday, December 17, 2019 7:10 PM
  • User-284642143 posted
    I'm not sure I understand. So the Post request is deserialised and saved to the database successfully. I know the URL to redirect to which is in the ashx file.

    I have redirected other ashx handlers to an aspx page but this time I think it's because I'm processing it differently.

    Could I create some JavaScript in the ashx file using a StringBuilder or similar to execute that once the process is finished?
    Tuesday, December 17, 2019 7:19 PM
  • User475983607 posted

    I'm not sure I understand. So the Post request is deserialised and saved to the database successfully. I know the URL to redirect to which is in the ashx file.

    I have redirected other ashx handlers to an aspx page but this time I think it's because I'm processing it differently.

    Could I create some JavaScript in the ashx file using a StringBuilder or similar to execute that once the process is finished?

    A technology like SinglaR, polling, or long polling is required. 

    Tuesday, December 17, 2019 7:35 PM
  • User-284642143 posted

    I dont think i will pick up those languages quickly to redo this task :-(...

    What i tried is the below which loads the URL set. I cant test this now as i dont have the project infront of me but on my home computer i created a web project, added an ashx handler (Generic) and added this to ProcessRequest which redirect successfully

    var url = "https://www.microsoft.com/en-gb/";
    
                context.Response.Clear();
                var sb = new System.Text.StringBuilder();
                sb.Append("<html>");
                sb.AppendFormat("<body onload='document.forms[0].submit()'>");
                sb.AppendFormat("<form action='{0}' method='post'>", url);
                sb.Append("</form>");
                sb.Append("</body>");
                sb.Append("</html>");
                context.Response.Write(sb.ToString());
                context.Response.End();

    This does the trick but as i say i cant test it against the correct project, but if you feel this may introduce new issues please do advise? Im not sure how the context may respond in this manner

    Tuesday, December 17, 2019 7:56 PM
  • User753101303 posted

    Hi,

    And the other side is an Ajax request or a browser level POST ??? If Ajax, the response is exposed programmatically as well without any browser interference (ie if looking at the response programmatically you would likely see the markup for the page to which you redirected this request but the browser will never decide to show that in its main window)

    If confirmed it's likely best to not redirect and to let the other side do whatever they want once the Ajax request is completed such using https://developer.mozilla.org/en-US/docs/Web/API/Location/assign to ask the browser to show some other page in its window.

    If this is really a browser level request I don't see why redirect would not work (make sure to not return any payload before)

    Tuesday, December 17, 2019 8:00 PM
  • User475983607 posted

    As I understand, a 3rd party POST information to the the ashx.  This information is stored in the database.  Later a user make a request to the same ashx handler and is somehow redirected to another page.

    The part that is not clear is how does the user know when to request the ashx URL?  When the user does make the request what information is used to redirect the user?

    Tuesday, December 17, 2019 8:05 PM
  • User-284642143 posted

    Ok, let me clarify (i tried to keep it short but I'll clarify now hopefully not adding any more complexity):

    User adds product to their cart.
    User Checks out and pays for their product.
    The payment gateway sends a Response back in JSON with the Body Request.
    I process this request by deserialising the data and taking action i.e. if successful and the record has not already been processed then add to database etc.
    Now i redirect the user to a thankyou page. The thank you page is never reached

    I set the thank you page URL as per the documentation this is the URL to process the transaction. In the past i have done the same but the Handler lets me redirect to thank you page. I THINK due to the POST it doesnt.

    @Patrice - Not sure if your comment was based on the code i posted?

    Tuesday, December 17, 2019 8:12 PM
  • User475983607 posted

    This design will not work for the reason already explained.   Request/response are isolated from each other.   You'll need to either poll to find the result of the purchase or change the design to make the payment during the user's request.

    Tuesday, December 17, 2019 8:25 PM
  • User-284642143 posted

    Ok... just exhausting other alternatives..... I cant use HttpWebRequest to create a Request and force redirection to another URL?

    Not sure how i could determine if the payment is successful at any other stage. I will check if they have a poll facility but doubt it

    Tuesday, December 17, 2019 9:06 PM
  • User475983607 posted

    I will check if they have a poll facility but doubt it

    No, this is code you need to design and write not the payment gateway.  

    Tuesday, December 17, 2019 9:10 PM
  • User-284642143 posted
    Another thought..... If I convert this ashx to a standard aspx web page.... That may do the trick? My only concern is how the post data would be handled coming to this page?? Would it check at page load for ALL the types of requests coming - I think they send multiple requests with a mixture of Get and Post
    Tuesday, December 17, 2019 9:23 PM
  • User475983607 posted

    Another thought..... If I convert this ashx to a standard aspx web page.... That may do the trick? My only concern is how the post data would be handled coming to this page?? Would it check at page load for ALL the types of requests coming - I think they send multiple requests with a mixture of Get and Post

    The file extension makes no difference.  

    It seems like your payment gateway or at least the process you selected does a call back.  It is up to you to handle the call back.  I assume the callback has customer information that can be used to lookup the user.  However, the user must make one more request so you can execute code and lookup the results of the callback.

     

    Tuesday, December 17, 2019 9:40 PM
  • User753101303 posted

    For now I'm rather trying to understand who is doing this POST request to your ASHX handler. From your last post, it seems this query could come directly from the 3rd party payment gateway server and so you would send this redirect to this server rather than to the user browser ?

    Don't you have a public documentation that would explain the whole flow ? If confirmed, I would expect the browser being redirected using either a parameter posted to the payment gateway or maybe a location configured on the payment gateway itself rather than as a response to a "their server to your server" POST request.

    Tuesday, December 17, 2019 11:30 PM
  • User-284642143 posted

    @mgebhard - I could look up the user from my database but i dont understand how i would then redirect?
    @Patrice - Heres the link https://stripe.com/docs/webhooks/build im NOT using MVC. 

    To clarify after a successful payment:

    Data is sent to my ashx Handler. I wait for the correct POST data which has the succeed event (Events.PaymentIntentSucceeded).
    My database operations are completed and successful.
    I want to direct the user to a thanks page - this is the problem.

    Thanks guys

    Wednesday, December 18, 2019 11:31 AM
  • User753101303 posted

    So to me it confirms this http request is done by their server (rather than by the user browser) and so sending back a redirect won't have any effect on the user navigation.

    Instead it seems to me it should be handled on the client side using something such as https://stripe.com/docs/payments/accept-a-payment#web-submit-payment when testing the result of the confirmCardPayment.

    This is "after" or "during" ? It lacks some detail but my understanding is that :
    - confirmCardPayment is called doing a call to their server
    - their server calls your webhook endpoint to notify directly your server about this payment
    - then their server returns a response to the browser and you can then check the result of the confirmCardPayment call and act accordingly depending on what happened (such as directing the user to some other page if the payment is successfull)

    In short I would test something such as:

    <payment code> etc...
    .then(function(result) { debugger; // is this breakpoint reached after the ASHX call ? if (result.error) { // inform the user about the error... } else { // The payment has been processed! if (result.paymentIntent.status === 'succeeded') { // I would handle the redirect here document.location.replace("/mypage.aspx"); // or preferably a server side generated absolute url }
    // else ? } }); });

    Edit: the ASHX call may perhaps happen slightly after the client side call ends? It shouldn't really change how to handle the redirection.

    Wednesday, December 18, 2019 12:09 PM
  • User475983607 posted

    Web hooks are a method to connect applications.  In this case the webhook notifies your web server.   It is up to you to design and write code to notify the client.  I'm mot sure how to explain this any other way.  It seems you do not understand how web site work fundamentally which caused you to go down the wrong path.

    Wednesday, December 18, 2019 12:19 PM
  • User-284642143 posted
    @patrice - thanks I'll have a look to see how I can implement this. Cheers
    @mgebhard - I did what I thought was right compared to other similar systems. If I've not understood you, there's a reason for that. You should at times look at the other side of the coin!
    Wednesday, December 18, 2019 2:43 PM