【发布时间】:2014-10-18 14:49:48
【问题描述】:
stripe.net 文档说您应该按如下方式处理错误:
任何服务上发生的任何错误都将引发 StripeException,并带有从 Stripe 返回的消息。在 try 中运行服务调用并捕获 StripeException 是个好主意。
您如何处理捕获错误并返回视图。
如果chargeService.Create失败并出现错误如何将对象stripeCharge返回给View:return View(stripeCharge);
public ActionResult Create(StripeCharge stripeCharge)
{
if (ModelState.IsValid)
{
var myPlan = new StripeChargeCreateOptions();
myPlan.Amount = stripeCharge.Amount;
try
{
var chargeService = new StripeChargeService();
StripeCharge response = chargeService.Create(myPlan);
}
catch (Exception e)
{
errorMessage = e.Message;
}
return RedirectToAction("Index");
}
return View(stripeCharge);
}
更新 经过进一步研究,这可能是solution
public ActionResult Create(StripeCharge stripeCharge)
{
if (ModelState.IsValid)
{
var myPlan = new StripeChargeCreateOptions();
myPlan.Amount = stripeCharge.Amount;
try
{
var chargeService = new StripeChargeService();
StripeCharge response = chargeService.Create(myPlan);
return RedirectToAction("Index");
}
catch (Exception e)
{
errorMessage = e.Message;
return View(stripeCharge);
}
}
return View(stripeCharge);
}
【问题讨论】:
标签: asp.net-mvc try-catch stripe.net