【问题标题】:using ControllerContext.RequestContext in a web api controller在 Web api 控制器中使用 ControllerContext.RequestContext
【发布时间】:2013-04-30 13:33:43
【问题描述】:

我正在尝试将 SagePayMvc.dll 集成到一个 ASP.NET Web API 项目中,该项目需要传递 ControllerContext.RequestContext 以形成通知 URL。

目前我在实现这一点时遇到了一些困难,我需要从这个 web api 控制器传递ControllerContext.RequestContext

public class PaymentStartController : ApiController
{
    private PaymentRepository paymentRepository = new PaymentRepository();
    private SagePayHelper sagePayHelper = new SagePayHelper();

    public Order MakePaymentInitial(Payment payment)
    {
        Order order = new Order();

        order = sagePayHelper.MakePayment(payment, context);
        paymentRepository.InsertVendorTXCode(order.VendorTxCode);
        paymentRepository.InsertInitialPaymentDetails(order, payment);
        return order;
    }

}

我尝试在 SagePayHelper 实例化下方添加一个公共 ControllerContext controllerContext = new ControllerContext(),然后随后添加了 var context = controllerContext.RequestContext,问题在于 RequestContext 内的任何方法都没有被实例化,所以当 SagePayMvc 到达构建点时在IUrlResolver 接口内完成的通知网址会引发错误。

有没有办法模拟ControllerContext.RequestContext,我以前使用过RhinoMocks,或者恢复到我之前在表单项目中实现SagePayMvc的方式会更谨慎(表单项目是一个MVC 4应用程序,可以序列化并将表单数据发送到 web api)。

任何建议将不胜感激。

【问题讨论】:

    标签: c# asp.net-web-api mocking requestcontext controllercontext


    【解决方案1】:

    ASP.NET Web API 使用与 ASP.NET MVC 完全不同的运行时组件来表示上下文和请求/响应消息。看起来您使用的 API 与 ASP.NET MVC 密切相关,这使得在 ASP.NET Web API 中重用非常困难,除非您初始化 ASP.NET MVC 内容以进行手动映射。我认为您只使用 ASP.NET MVC 来调用期望 MVC 上下文的方法会更容易。

    【讨论】:

    • 决定将项目恢复到以前的版本,其中包括表单项目中的事务服务,工作正常感谢 Pablo 的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多