我可以在MVC项目中使用Glimpse显示Application或Cache对象的内容吗?

时间:2012-04-03 16:18:58

标签: asp.net-mvc-3 glimpse

ASP.NET WebForms跟踪输出有一个Application State部分。是否可以使用Glimpse看到相同的内容?

在我的家庭控制器的Index()方法中,我尝试添加一些测试值,但是我没有在任何Glimpse选项卡中看到输出。

ControllerContext.HttpContext.Application.Add("TEST1", "VALUE1");
ControllerContext.HttpContext.Cache.Insert("TEST2", "VALUE2");

我在文档中也没有看到任何内容。

1 个答案:

答案 0 :(得分:6)

我认为没有对此提供开箱即用的支持,但write a plugin显示此信息将是微不足道的。

例如,为了显示存储在ApplicationState中的所有内容,您可以编写以下插件:

[Glimpse.Core.Extensibility.GlimpsePluginAttribute]
public class ApplicationStateGlimpsePlugin : IGlimpsePlugin
{
    public object GetData(HttpContextBase context)
    {
        var data = new List<object[]> { new[] { "Key", "Value" } };
        foreach (string key in context.Application.Keys)
        {
            data.Add(new object[] { key, context.Application[key] });
        }
        return data;
    }

    public void SetupInit()
    {
    }

    public string Name
    {
        get { return "ApplicationState"; }
    }
}

然后你得到了想要的结果:

enter image description here

并列出存储在缓存中的所有内容:

[Glimpse.Core.Extensibility.GlimpsePluginAttribute]
public class ApplicationCacheGlimpsePlugin : IGlimpsePlugin
{
    public object GetData(HttpContextBase context)
    {
        var data = new List<object[]> { new[] { "Key", "Value" } };
        foreach (DictionaryEntry item in context.Cache)
        {
            data.Add(new object[] { item.Key, item.Value });
        }
        return data;
    }

    public void SetupInit()
    {
    }

    public string Name
    {
        get { return "ApplicationCache"; }
    }
}
相关问题