ASP.NET MVC : How to create own HttpContext

喜欢而已 提交于 2019-12-08 04:15:57

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/2299247/asp-net-mvc-how-to-create-own-httpcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!