Hitin
Hitin

Reputation: 442

Issue redirecting from ASPX page to MVC action

I am having trouble redirecting from aspx page to mvc action. Actually the aspx is a response handler for payment gateway. Which then based on response code redirects user to appropriate action. However I am having some issue during the redirection. Earlier I have tried to accept response on MVC action, however I got an error, therefore I decide to use aspx to handle response and redirect to mvc action.

Below is the code that aspx page has in it:

<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(Object sender, System.EventArgs e)
    {
        String result = System.Web.HttpContext.Current.Request["result"];
        String paymentID = System.Web.HttpContext.Current.Request["paymentid"];
        String respons = System.Web.HttpContext.Current.Request["responsecode"];
        String err = System.Web.HttpContext.Current.Request["Error"];
        String errmsg = System.Web.HttpContext.Current.Request["ErrorText"];
        String tid = System.Web.HttpContext.Current.Request["Trackid"];

        String query = String.Format("checkout/PaymentResult?result={0}&paymentid={1}&responsecode={2}&error={3}&errortext={4}&trackid={5}", result, paymentID, respons, err, errmsg, tid);

        var _file = new System.IO.StreamWriter(Server.MapPath("~/Response.log"), true);
        _file.WriteLine(query);
        _file.Close();
        //Below line is working fine in aspx pages
        System.Web.HttpContext.Current.Response.Write("Redirect=" + ConfigurationManager.AppSettings["BaseURL"].ToString() + query);

        //Tried below two, not working
        //System.Web.HttpContext.Current.Response.Redirect(ConfigurationManager.AppSettings["BaseURL"].ToString() + query);  
        //Response.RedirectPermanent(ConfigurationManager.AppSettings["BaseURL"].ToString() + query);
    }
</script>

Payment gateway response for mvc action response handler:
If I pass site URL with following "checkout/PaymentResult" I get below error.

09:38:40,872 FATAL event.com.aciworldwide.commerce.gateway.payment.MerchantNotificationService [TP-Processor5]  - Hack characer/length check failed on redirect URL:
         <!DOCTYPE html>
         <html lang="en">
         <head>
             <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
             <meta http-equiv="X-UA-Compatible" content="IE=9">
             <title>XXXXXXXss.com - Free home Delivery</title>

Also, I have noticed that in ASPX, the page gets called twice for some reason. First from payment gateway with all values, second time with blank values. See the log file details below.

checkout/PaymentResult?result=CAPTURED&paymentid=582000001361270&responsecode=00&error=&errortext=&trackid=300009
checkout/PaymentResult?result=&paymentid=&responsecode=&error=&errortext=&trackid=

UPDATE
Controller code:

public ActionResult PaymentResult(string result, string paymentid, string responsecode, string error, string errortext, string trackid)
        {
            var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
              .LimitPerStore(_storeContext.CurrentStore.Id)
              .ToList();

            // transaction response code
            String _result = (!string.IsNullOrEmpty(result)) ? result : "null";

            String _paymentId = (!string.IsNullOrEmpty(paymentid)) ? paymentid : "null";

            String _response = (!string.IsNullOrEmpty(responsecode)) ? responsecode : "null";

            String _err = (!string.IsNullOrEmpty(error)) ? error : "null";

            String _errMessage = (!string.IsNullOrEmpty(errortext)) ? errortext : "null";

            String _trackID = (!string.IsNullOrEmpty(trackid)) ? trackid : "null";

            // define message string for errors
            String _message = getResponseDescription(_response);
            Session["PaymentMessage"] = String.Format("Message: {0}, Code: {1}", _message, responsecode);

            try
            {

                var order = _orderService.GetOrderById(Convert.ToInt32(_trackID));
                if (_result.Equals("CAPTURED"))
                {
                    try
                    {
                        try
                        {
                            order.OrderNotes.Add(new OrderNote
                            {
                                Note = String.Format("Bank Response= {0}, Code= {1}", _result, _response),
                                DisplayToCustomer = false,
                                CreatedOnUtc = DateTime.UtcNow
                            });
                            _orderService.UpdateOrder(order);
                        }
                        catch (Exception ex)
                        {
                            LogException(ex);
                        }
                        cart.ToList().ForEach(sci => _shoppingCartService.DeleteShoppingCartItem(sci, false));
                        order.CaptureTransactionResult = _message;
                        _orderService.UpdateOrder(order);
                        _orderProcessingService.MarkOrderAsPaid(order);
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                    }

                    return RedirectToRoute("CheckoutCompleted", new { orderId = order.Id });
                }
                else
                {
                    _orderService.UpdateOrder(order);
                    order.CaptureTransactionResult = _message;
                    _orderProcessingService.CancelOrder(order, true);
                    return Redirect(Url.Action("Cart", "ShoppingCart"));
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                return Redirect(Url.Action("Cart", "ShoppingCart"));
            }

        }

I added, !IsPostBack, but it still wont do anything, the blank lines are still coming in.

Update 2:
I think the problem is that the session is getting lost after I redirect back from gateway to application. Also I am using a web garden, so I believe the worker process gets changed. However, I am using ASP.NET out of process session state.

How do I share session data across the web garden?

Upvotes: 0

Views: 1184

Answers (1)

AHL
AHL

Reputation: 23

The way we do redirection is we supply full url from webform application and not just the controller / action bit. So try appending the domain part e.g http://www.yoururl.com/checkout/paymentresult...

N.B Both our websites, mvc and webforms are deployed on separate iis sites with different port numbers. Both share session information.

Hope it helps.

Update: just noticed you are appending BaseUrl so there must be something else. :)

Also, I think we have to put if(!isPostBack()) for page load method. That can solve one problem of double posting

Upvotes: 1

Related Questions