ASP.NET MVC:如何创建自己的 HttpContext
我的想法是创建我自己的 HttpContext,其中将包含我们应用程序中使用的一些内部对象。所以我想我会简单地创建
public class FooHttpContextBase : HttpContextBase
{
public string Foo
{
get { return "Boo"; }
}
}
然后重写 HttpContext 属性:
public abstract class BaseController : Controller
{
private FooHttpContextBase context;
public BaseController()
{
context = new FooHttpContextBase();
}
override public HttpContextBase HttpContext
{
get { return context; }
}
}
但后来我意识到 HttpContext 不是虚拟的 - 所以它不能被重写。
嗯,你有什么建议?在 BaseController 中添加一些新属性?
提前致谢!
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"; }
}
}
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; }
}
}
But then I've realized HttpContext is not a virtual - so it cannot be overridden.
Well, what do you suggest? Add some new property into the BaseController?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您只需向基本控制器添加一个新属性即可共享您自己的上下文。这是完成这项工作最简单、最优雅、最快速的方式。
如果您使用某些 IOC,我建议您对其进行属性注入,请参阅 此链接了解详细信息。
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.