Search code examples
c#asp.net-corehttp-redirectpaypaltempdata

RedirectToAction is not working while navigating from another website


I am working on an asp.net core MVC project with PayPal integration. After completing the payment, the PayPal correctly redirect to the success URL (.../PayPal/PaymentSuccess). Please see the method given below:

PayPalController

    public class PayPalController 
     {
            public IActionResult PaymentSuccess()
            {
            //code for business logic here
            TempData["Amount"] = amount; 
            return RedirectToAction("PaymentCompleted", "Home");
            }
     }

HomeController

         public class HomeController
         {
                public IActionResult PaymentCompleted()
                {
                var amount = Convert.ToDecimal(TempData["Amount"]);
                //code for business logic here
                return View();
                }
         }

I have tried a payment using PayPal in the hosted environment. After completing the payment the PayPal successfully invoked the PaymentSuccess() method. In this method, we have added the business logic for database updation. After this we need to redirect to another page "PaymentCompleted" to show the payment success message.

The issue is that the redirection to the view page "PaymentCompleted" which is in another controller (HomeController) is not working after successfully executing the code in PaymentSuccess method.

But, I have tried to invoke the PaymentSuccess() method directly in the browser and now the redirection is working.

EDIT: I have used a 'TempData' on 'PaymentSuccess()' method for passing paid amount to display on the 'PaymentCompleted' view page.

Why does this happen? Can anyone specify the reason behind this problem?

Any help would be appreciable. Thank You!


Solution

  • Finally, I got the solution. The issue is happened because of the way we used the TempData. We have to serialize the amount before assigning it to TempData in ASP.Net Core.

    TempData["Amount"] = JsonConvert.SerializeObject(amount);
    

    Also, we have to deserialize it before retrieving the TempData. No need to deserialize if it is string. I just showing the procedure. You can follow the below code:

    var amount = JsonConvert.DeserializeObject<string>(TempData["Amount"].ToString());
    

    But in MVC application, we don't need any serialization before assigning data to TempData.

    Now the RedirectionToAction() method worked and I retrieved the paid amount on the PaymentCompleted() method using TempData.