如何用asp.net mvc更改网址?

时间:2009-10-15 19:10:26

标签: c# .net asp.net-mvc

我想知道这是否可以轻松完成。我正在做一些paypal的东西,当用户从paypal网站返回时,他们会转到你将用户发送到paypal之前设置的“成功页面”。

所以现在我给我的客户2个选择。他们可以一次性付款,也可以定期付款。

现在有了paypal express,你必须在他们回到你的成功页面后打电话才能完成付款。

事情虽然是一次性付款需要不同的字段,然后定期付款和paypal并不容易说出选择了什么样的付款。你要么必须传递自己的自定义字段,然后再进行if语句检查,要么尝试做我正在做的事情。

所以正在发生的事情的样本就是这个。

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.

所以这就是我的success1视图的样子

public actionresult Success1()
{
    // some paypal stuff

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

所以基本上我所做的就是创造了一个名为“成功”的视图,并在其中看到了这样的部分视图

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

所以我的想法是,如果我有一个名为“成功”的视图,我告诉其他2个视图方法加载该视图然后我会得到我想要的。

这会将我的代码拆分为2个视图,但对于客户,他们总是会看到一个网址http://www.mysite.com/Success而不是http://www.mysite.com/Success1http://www.mysite.com/Success2

但这还不是100%有效。它会加载正确的局部视图和内容,但网址不会更改。我原本以为,由于我调用了不同的视图,它会将URL更改为该视图。

它似乎不那么有用。

1 个答案:

答案 0 :(得分:1)

您选择的视图不是确定网址,而是已执行的控制器操作。因此,例如,如果已执行Succcess1操作,则无论您在内部呈现什么视图,用户都将在其浏览器中看到http://www.mysite.com/controller/Success1

我建议您使用以下网址:

您的控制器操作可能如下所示:

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();
}