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

Mat*_*tio 1 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");
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*rov 6

我不认为对此有一个开箱即用的支持,但编写一个显示此信息的插件是微不足道的.

例如,为了显示存储在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"; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

[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"; }
    }
}
Run Code Online (Sandbox Code Playgroud)