ASP.NET MVC:如何创建自己的HttpContext [英] ASP.NET MVC : How to create own HttpContext
问题描述
我的想法是创建自己的HttpContext,其中将包括一些在我们的应用程序使用的内部对象。所以我想我会简单地创建
My idea was to create my own HttpContext which will include some of the internal objects used in our app. So I thought I will simply create
public class FooHttpContextBase : HttpContextBase
{
public string Foo
{
get { return "Boo"; }
}
}
,然后覆盖的HttpContext属性:
And then override HttpContext property:
public abstract class BaseController : Controller
{
private FooHttpContextBase context;
public BaseController()
{
context = new FooHttpContextBase();
}
override public HttpContextBase HttpContext
{
get { return context; }
}
}
但后来我意识到的HttpContext是不是一个虚拟的 - 所以它不能被重写
But then I've realized HttpContext is not a virtual - so it cannot be overridden.
好了,你有什么建议?添加一些新的属性到BaseController?
Well, what do you suggest? Add some new property into the BaseController?
在此先感谢!
推荐答案
我建议你只需要添加一个新的属性,以你的基地控制器将分享自己的上下文。这是最简单的,优雅的和快速的方式来完成这项工作。结果
如果您使用的是一些国际奥委会,我建议你做一个属性注入它,看到的此链接了解详情。
I recommend you just add a new property to your base controller that will share your own context. It's the most easy, elegant and fast way to do the job.
If you are using some IOC, I recommend you to do a property injection for it, see this link for details.
这篇关于ASP.NET MVC:如何创建自己的HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!