如何将 HttpContext 对象从控制器传递到 MVC 中的存储库类
我有一个带有构造函数的存储库类,该构造函数接受 HttpContext 对象作为其参数。如何将 HttpContext
对象从控制器传递到 MVC 项目中的此存储库类?
I have a repository class with a constructor which accepts an HttpContext
object as its parameter. How can I pass a HttpContext
object from a controller to this repository class in my MVC project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想在控制器操作中获取当前的 HttpContext,你可以这样做:
虽然我必须添加,但这听起来像是有问题的设计。我想不出存储库类直接需要上下文的任何原因。如果我需要上下文中的数据进入模型,我将创建一个值对象(也称为数据传输对象),根据需要序列化 HttpContext 并将其传递到存储库中。
If you want to get the current HttpContext in a controller action, you can do this:
Although I have to add, this sounds like problematic design. I can't think of any reason why a repository class would need the context directly. If I needed data from my context to go into my model, I would create a value object (aka data transfer object), serialize the HttpContext as required and pass that into the repository.