开发者

How can I change a url with asp.net mvc?

开发者 https://www.devze.com 2022-12-08 20:37 出处:网络
I am wondering if this can be done easily. I am doing some paypal stuff where when the user returns from the paypal site they go to a \"success page\" that you set before you send the user to paypal.

I am wondering if this can be done easily. I am doing some paypal stuff where when the user returns from the paypal site they go to a "success page" that you set before you send the user to paypal.

So now I give my customers 2 choices. They can do a one time payment or they can do a recurring payment.

Now with paypal express you have to do a call after they come back to you success page to finish the payment.

The thing though is a one time payment requires different fields then a recurring payment and paypal does not make it that easy to tell what kind of payment was chosen. You either have to pass your own custom field in and then do a if statement checks later or try to do what I am doing开发者_C百科.

So a sample of what is happening is this.

Customer: chooses recurring or one time payment
MyCode: sets up all needed variables for each one
            - If one time payment is selected then success url would be Http://www.mysite.com/Success1
            - If recurring is select then success url would be Http://www.mysite.com/Success2

Customer: Logs into paypal account and pays
PayPal: sends them to my success url either Success1 or Sucess2 method in my controller.

So this is how my success1 view would look like

public actionresult Success1()
{
    // some paypal stuff

    ViewData["NameOfPartialView"] = "Success1";
    return View("Success");
}

So basically what I did was I made a view called "Success" and stuck a partial view that looks like this in it

 <% Html.RenderPartial(ViewData["NameOfPartialView"].ToString()); %>

So my thinking was this if I have a view called "Success" and I tell the 2 other views methods to load that view up then I will get what I want.

this would split my code apart into 2 views but to the customer they will always just see one url http://www.mysite.com/Success instead of http://www.mysite.com/Success1 or http://www.mysite.com/Success2

But this does not work 100% yet. It loads up the right partial view and stuff but the url does not change. I would have thought that since I am calling a different view it would change the url to that view.

It does not seem to work that way.


It is not the View you are choosing that determines the url but the controller action that has been executed. So for example if the Succcess1 action has been executed, no matter what view you rendered inside, the user will see http://www.mysite.com/controller/Success1 in her browser.

I would suggest you the following urls:

  • If one time payment is selected success url would be http://www.mysite.com/SomeController/success/1
  • If recurring payment is selected success url would be http://www.mysite.com/SomeController/success/2

And your controller action might look like this:

public ActionResult Success(string id) 
{
    // Maybe perform some logic based on the id parameter
    TempData["paymentType"] = id;
    return RedirectToAction("Index");
}

public ActionResult Index()
{
    ViewData["paymentType"] = TempData["paymentType"];
    return View();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号